WPF、Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异的,此时我们就需要定制自己的Converter。
.Net Framework提供了两种Converter接口,单值转换的接口IValueConverter和多值转换的接口IMultiValueConverter,它们都属于System.Windows.Data命名空间,在程序集PresentationFramework.dll中。这两种值转换器都是分区域性的。其中方法Convert和ConvertBack都具有指示区域性信息的culture参数。如果区域性信息与转换无关,那么在自定义转换器中可以忽略该参数。
一、单值转换实例,IValueConverter
1.当值从绑定源传播给绑定目标时,调用方法Convert
2.当值从绑定目标传播给绑定源时,调用此方法ConvertBack,方法ConvertBack的实现必须是方法Convert的反向实现。
/// <summary>
/// 自定义事件转换
/// </summary>
public class TimeConver : IValueConverter
{
//当值从绑定源传播给绑定目标时,调用方法Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
DateTime date = (DateTime)value;
return date.ToString("yyyy-MM-dd");
}
//当值从绑定目标传播给绑定源时,调用此方法ConvertBack
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string str = value as string;
DateTime txtDate;
if (DateTime.TryParse(str, out txtDate))
{
return txtDate;
}
return DependencyProperty.UnsetValue;
}
}
注:返回值DependencyProperty.UnsetValue表示转换器没有生成任何值。
在xaml中引用TimeConver的命名空间
xmlns:local="clr-namespace:AudioDemo.View"
在xaml中定义Resources
<Window.Resources>
<local:TimeConver x:Key="cvtDate"/>
</Window.Resources>
在xaml重指定Binding值使用自定义Converter转换
<TextBox x:Name="textBox" Text="{Binding ElementName=dateOne,Path=SelectedDate,Converter={StaticResource cvtDate}}"
HorizontalAlignment="Left"
Height="23" Margin="85,105,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="183"/>
Xaml文件内容:
<Grid>
<DatePicker x:Name="dateOne"
HorizontalAlignment="Left" Margin="85,50,0,0" VerticalAlignment="Top" Width="183"
SelectedDateFormat="Long"/>
<TextBox x:Name="textBox" Text="{Binding ElementName=dateOne,Path=SelectedDate,Converter={StaticResource cvtDate}}"
HorizontalAlignment="Left"
Height="23" Margin="85,105,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="183"/>
<Label x:Name="label" Content="选择结果:" HorizontalAlignment="Left" Margin="19,105,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="{Binding ElementName=dateOne,Path=Text}"
HorizontalAlignment="Left" Margin="85,145,0,0" VerticalAlignment="Top"/>
</Grid>
运行结果:
使用转换前: 使用自定义Converter转换后:


需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同。
一、MultiBinding+Converter 多值绑定及多值转换实例
当纵向流量大于横向流量时指示灯应为绿色,当纵向流量小于横向流量时指示灯应为红色,否则指示灯为黄色。
1、定制ColorConverter类,此时Convert中参数是object[] values,values[0]对应MultiBinding中的第一个Binding值,这里是纵向流量值,依此类推,可以在MultiBinding对象中指定多个绑定。
public class ColorConverter : IMultiValueConverter
{
//正向修改
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
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;
}
}
2.Xaml定义
添加命名空间
xmlns:local="clr-namespace:AudioDemo.View"
<Window.Resources>
<local:ColorConverter 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>

二、RGB颜色混合实例
1.转换器定义
public class RGBConverter : IMultiValueConverter
{
//正向修改,整合颜色值
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 3)
return null;
byte r = System.Convert.ToByte(values[0]);
byte g = System.Convert.ToByte(values[1]);
byte b = System.Convert.ToByte(values[2]);
Color col = Color.FromRgb(r, g, b);
SolidColorBrush brush = new SolidColorBrush(col);
return brush;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
2.Xaml定义
别忘先添加命名空间
xmlns:local="clr-namespace:AudioDemo.View"
<Window.Resources>
<local:RGBConverter x:Key="rgbCvt"/>
</Window.Resources>
<Grid>
<Label x:Name="label" Content="Red:" HorizontalAlignment="Left" Margin="10,48,0,0" VerticalAlignment="Top"/>
<Label x:Name="label_Copy" Content="Green:" HorizontalAlignment="Left" Margin="7,85,0,0" VerticalAlignment="Top"/>
<Label x:Name="label_Copy1" Content="Blue:" HorizontalAlignment="Left" Margin="7,123,0,0" VerticalAlignment="Top"/>
<Slider x:Name="slider_r" Minimum="0" Maximum="255" Ticks="1" HorizontalAlignment="Left" Margin="68,53,0,0" VerticalAlignment="Top" Width="207"/>
<Slider x:Name="slider_g" Minimum="0" Maximum="255" Ticks="1" HorizontalAlignment="Left" Margin="68,91,0,0" VerticalAlignment="Top" Width="207"/>
<Slider x:Name="slider_b" Minimum="0" Maximum="255" Ticks="1" HorizontalAlignment="Left" Margin="68,124,0,0" VerticalAlignment="Top" Width="207"/>
<Rectangle HorizontalAlignment="Left" Height="90" Margin="68,160,0,0" Stroke="Black" VerticalAlignment="Top" Width="142">
<Rectangle.Fill>
<MultiBinding Converter="{StaticResource rgbCvt}">
<Binding ElementName="slider_r" Path="Value"></Binding>
<Binding ElementName="slider_g" Path="Value"></Binding>
<Binding ElementName="slider_b" Path="Value"></Binding>
</MultiBinding>
</Rectangle.Fill>
</Rectangle>
</Grid>
运行结果:

转载自:https://www.cnblogs.com/tianma3798/p/5927470.html

浙公网安备 33010602011771号