值转换分为单值转换IValueConverter和多值转换IMultiValueConverter。
使用值转化类可以简单轻松的解决前台界面显示数据的变化,解决同一种数据多种表示的难题。
一、单值转换IValueConverter
1.首先建立一个转换类,引用using System.Windows.Data;,然后继承IValueConverter,点击实现接口
2.补充完整逻辑后
class YesNoToConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//value就是前台传递过来的值
switch (value.ToString().ToLower())
{
case "y":
case "ye":
case "yes":
return true;
case "n":
case "no":
case "non":
return false;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return "yes";
else
return "no";
}
return "no";
}
}
3.前台界面
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:YesNoToConverter x:Key="yn"/>
</Window.Resources>
<StackPanel Margin="10">
<TextBox Name="txtValue" />
<WrapPanel Margin="0,10">
<TextBlock Text="Current value is: " />
<TextBlock Name="YN" Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource yn}}"></TextBlock>
</WrapPanel>
<CheckBox IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource yn}}" Content="{Binding ElementName=YN,Path=Text}" />
</StackPanel>
</Window>
前台界面Window.Resources这里需要引用。然后对应的控件进行绑定。
4.效果图
输入y ye yes 都是YES,输入n no non都是NO,这个是你在代码里面设置的。
二、多值转换IMultiValueConverter
1.首先建立一个转换类,引用using System.Windows.Data;,然后继承IMultiValueConverter,点击实现接口
2.补充完整逻辑后
class YesNoToConverter2 : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//这里的values就是多个值,与前台绑定的数据有关,有几个值,就判断几个值
if (values == null || values.Length < 2)
return DependencyProperty.UnsetValue;
double verValue = (double)values[0];
double horValue = (double)values[1];
if (verValue > horValue)
return new SolidColorBrush(Colors.Green);
else if (verValue < horValue)
return new SolidColorBrush(Colors.Red);
return new SolidColorBrush(Colors.Yellow);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
3.前台界面
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:YesNoToConverter2 x:Key="cvtColor"/>
</Window.Resources>
<Grid>
<Label x:Name="label" Content="纵向值:" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="横向值:" HorizontalAlignment="Left" Margin="10,80,0,0" VerticalAlignment="Top"/>
<Slider x:Name="sliderVer" HorizontalAlignment="Left" Margin="75,43,0,0" VerticalAlignment="Top" Width="192"/>
<Slider x:Name="sliderHor" HorizontalAlignment="Left" Margin="75,81,0,0" VerticalAlignment="Top" Width="192"/>
<Ellipse HorizontalAlignment="Left" Height="100" Margin="75,120,0,0" Stroke="Black" VerticalAlignment="Top" Width="100">
<Ellipse.Fill>
<MultiBinding Converter="{StaticResource cvtColor}">
<Binding Path="Value" ElementName="sliderVer"/>
<Binding Path="Value" ElementName="sliderHor"/>
</MultiBinding>
</Ellipse.Fill>
</Ellipse>
</Grid>
</Window>
拓展:
多值绑定,根据Select下拉框的值来改变Name中背景颜色。
其中,Name和Select都要有属性通知,多值绑定
Select下拉框需要进行事件绑定数据
<TextBox Style="{StaticResource dgTextBox}"
Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}">
<TextBox.Background>
<MultiBinding Converter="{StaticResource relayIPColorConverter}">
<Binding Path="Name" />
<Binding Path="Select" />
</MultiBinding>
</TextBox.Background>
</TextBox>
private void ComboBox_SelectionChanged_2(object sender, SelectionChangedEventArgs e)
{
var current = ((sender as ComboBox).DataContext) as StudentInfo;
switch (((ContentControl)e.AddedItems[0]).Content)
{
case "a": current.Type = 1; break;
case "b": current.Type = 2; break;
case "c": current.Type = 3; break;
}
}
4.效果图