WPF使用转换器(Converter)

1.作用:可以将源数据和目标数据之间进行特定的转化,

2.定义转换器,需要继承接口IValueConverter

[ValueConversion(typeof(int), typeof(string))]
public class ForeColorConverter : IValueConverter
{
    //源属性传给目标属性时,调用此方法ConvertBack
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int c = System.Convert.ToInt32(parameter);
 
        if (value == null)
            throw new ArgumentNullException("value can not be null");
 
        int index = System.Convert.ToInt32(value);
        if (index == 0)
            return "Blue";
        else if (index == 1)
            return "Red";
        else
            return "Green";
    }
 
    //目标属性传给源属性时,调用此方法ConvertBack
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
} 

public ValueConversionAttribute(Type sourceType, TypetargetType):指定源属性类型和目标属性类型

Convert:会进行源属性传给目标属性的特定转化

ConvertBack:会进行目标属性传给源属性的特定转化

加粗样式参数parameter:对应Binding的ConverterParameter属性

3.使用转换器

(1)引用转换器所在的命名空间

xmlns:local1="clr-namespace:WpfTest.View"

(2)定义资源

<Window.Resources>
    <local1:ForeColorConverter x:Key="foreColor"></local1:ForeColorConverter>
</Window.Resources>

(3)定义属性

private int status = 0;
public int Status
{
    get => status; set { status = value; RaisePropertyChanged("Status"); }
 
}

(4)绑定属性,添加转换器

<Grid>
    <Label HorizontalAlignment="Left" Height="23" Margin="243,208,0,0" Content="这里哦" Foreground="{Binding Status,Converter={StaticResource foreColor},Mode=OneWay}" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="tbName" HorizontalAlignment="Left" Height="23" Margin="243,160,0,0" TextWrapping="Wrap" Text="{Binding Status,UpdateSourceTrigger=LostFocus,Mode=OneWayToSource}" VerticalAlignment="Top" Width="120"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="389,160,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

4.效果

WPF使用转换器(Converter)_第1张图片
WPF使用转换器(Converter)_第2张图片

WPF使用转换器(Converter)_第3张图片

其它示例:
1、定义转换器,需要继承接口IValueConverter

public class LongToBoolConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return new NotImplementedException();
            }

            double vv = (string)value == "" ? 0 : double.Parse((string)value);

            return  vv > 8 ? true : false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

2、定义资源

<bandit:BanditWindow.Resources>
    <localHelper:LongToBoolConverter x:Key="LongToBoolConverter"/>
</bandit:BanditWindow.Resources>

3、数据绑定

<TextBox Width="200" Height="50" Name="txtBoxName" VerticalContentAlignment="Center"> 
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=txtBoxName,Path=Text,Converter={StaticResource LongToBoolConverter}}" Value="True" >
                                    <Setter Property="Background" Value="Green"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=txtBoxName,Path=Text,Converter={StaticResource LongToBoolConverter}}" Value="False" >
                                    <Setter Property="Background" Value="Red"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                            
                        </Style>
                    </TextBox.Style>
                </TextBox>

4、效果展示:
在这里插入图片描述
在这里插入图片描述

你可能感兴趣的:(WPF,wpf,c#,开发语言)