WPF转换器 IValueConverter
转换器 IValueConverter 接口
定义:
提供将自定义逻辑应用于绑定的方法。
如果要将值转换器与绑定相关联,请创建一个实现接口的类继承IValueConverter,然后实现 Convert 和 ConvertBack 方法。 转换器可将数据从一种类型更改为另一种类型,根据区域性信息转换数据,或修改演示的其他方面。
值转换器识别区域性。 Convert和 ConvertBack 方法都有一个 culture 指示区域性信息的参数。 如果区域性信息与转换无关,则可在自定义转换器中忽略该参数。
Convert和 ConvertBack 方法也有一个名为parameter的参数, parameter 以便你可以使用具有不同参数的转换器的同一实例。 例如,你可以编写一个格式转换器,该转换器根据你使用的输入参数生成不同的数据格式。 您可以使用 ConverterParameter Binding 类的来将参数作为参数传递到 Convert 和 ConvertBack 方法。
方法:
Convert(Object, Type, Object, CultureInfo) 转换值。
ConvertBack(Object, Type, Object, CultureInfo) 转换值。
#region 程序集 PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\PresentationFramework.dll
#endregion
using System.Globalization;
namespace System.Windows.Data
{
//
// 摘要:
// 提供将自定义逻辑应用于绑定的方法。
public interface IValueConverter
{
//
// 摘要:
// 转换值。
//
// 参数:
// value:
// 绑定源生成的值。
//
// targetType:
// 绑定目标属性的类型。
//
// parameter:
// 要使用的转换器参数。
//
// culture:
// 要用在转换器中的区域性。
//
// 返回结果:
// 转换后的值。 如果该方法返回 null,则使用有效的 null 值。
object Convert(object value, Type targetType, object parameter, CultureInfo culture);
//
// 摘要:
// 转换值。
//
// 参数:
// value:
// 绑定目标生成的值。
//
// targetType:
// 要转换为的类型。
//
// parameter:
// 要使用的转换器参数。
//
// culture:
// 要用在转换器中的区域性。
//
// 返回结果:
// 转换后的值。 如果该方法返回 null,则使用有效的 null 值。
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
}
编写转换器 继承IValueConverter接口:
using System;
using System.Globalization;
using System.Windows.Data;
namespace ConverterDemo.Converter
{
/// <summary>
/// 性别转换器1
/// </summary>
public class GenderConverter1 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return "男";
return value.ToString() == parameter.ToString() ? "男" : "女";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString() == "男" ? 0 : 1;
}
}
/// <summary>
/// 性别转换器2
/// </summary>
public class GenderConverter2 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
return value.ToString() == parameter.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return parameter;
}
}
}
Convert:
参数
value Object
绑定源生成的值。
targetType Type
绑定目标属性的类型。
parameter Object
要使用的转换器参数。
culture CultureInfo
要用在转换器中的区域性。
返回
Object
转换后的值。 如果该方法返回 null,则使用有效的 null 值。
注解
数据绑定引擎在将值从绑定源传播到绑定目标时会调用此方法。
数据绑定引擎不会捕获由用户提供的转换器引发的异常。 方法所引发的任何异常 Convert ,或方法调用的方法所引发的任何未捕获异常 Convert 均被视为运行时错误。 通过返回来处理预期问题 DependencyProperty.UnsetValue 。
的返回值 DependencyProperty.UnsetValue 指示转换器没有生成值,并且绑定将使用 FallbackValue (如果可用)或默认值。
返回值 Binding.DoNothing 指示绑定不传输值或使用 FallbackValue 或默认值。
示例:
using System.ComponentModel;
namespace ConverterDemo
{
public class ConverterViewModel : INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private int _gender;
/// <summary>
/// 测试数据 性别:男 0 、女 1
/// </summary>
public int Gender
{
get { return _gender; }
set
{
_gender = value;
OnPropertyChanged("Gender");
}
}
}
}
using System.Windows;
namespace ConverterDemo
{
/// <summary>
/// ConverterPage.xaml 的交互逻辑
/// </summary>
public partial class ConverterPage : Window
{
ConverterViewModel viewModel;
public ConverterPage()
{
InitializeComponent();
viewModel = new ConverterViewModel();
this.DataContext = viewModel;
}
}
}
<Window x:Class="ConverterDemo.ConverterPage"
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:ConverterDemo"
mc:Ignorable="d"
xmlns:converter ="clr-namespace:ConverterDemo.Converter"
Title="ConverterPage" Height="450" Width="800">
<Window.Resources>
<converter:GenderConverter1 x:Key="GenderConverter1"/>
<converter:GenderConverter2 x:Key="GenderConverter2"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="120,10,0,0">
<TextBlock Text="未使用转换器 :"/>
<TextBlock Margin="10,0,0,0" Text="{Binding Gender,Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="120,50,0,0">
<TextBlock Text="使用转换器1 :"/>
<TextBox Width="60" Margin="10,0,0,0" Text="{Binding Gender,Mode=TwoWay,Converter={StaticResource GenderConverter1},ConverterParameter=0}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="120,90,0,0">
<TextBlock Text="使用转换器2 :"/>
<RadioButton Content="男" Margin="10,0,0,0"
IsChecked="{Binding Gender,Converter={StaticResource GenderConverter2},ConverterParameter=0}"/>
<RadioButton Content="女" Margin="10,0,0,0"
IsChecked="{Binding Gender,Converter={StaticResource GenderConverter2},ConverterParameter=1}"/>
</StackPanel>
</Grid>
</Window>
/// <summary>
/// 性别转换器1
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return "男";
return value.ToString() == parameter.ToString() ? "男" : "女";
}
/// <summary>
/// 性别转换器2
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
return value.ToString() == parameter.ToString();
}
ConvertBack:
参数
value Object
绑定目标生成的值。
targetType Type
要转换为的类型。
parameter Object
要使用的转换器参数。
culture CultureInfo
要用在转换器中的区域性。
返回
Object
转换后的值。 如果该方法返回 null,则使用有效的 null 值。
注解
数据绑定引擎在将值从绑定目标传播到绑定源时调用此方法。
此方法的实现必须是方法的反向实现 Convert 。
数据绑定引擎不会捕获由用户提供的转换器引发的异常。 方法所引发的任何异常 ConvertBack ,或方法调用的方法所引发的任何未捕获异常 ConvertBack 均被视为运行时错误。 通过返回来处理预期 DependencyProperty 问题。 UnsetValue
的返回值 DependencyProperty.UnsetValue 指示转换器没有生成值,并且绑定将使用 FallbackValue (如果可用)或默认值。
返回值 Binding.DoNothing 指示绑定不传输值或使用 FallbackValue 或默认值。
代码:
/// <summary>
/// 性别转换器1
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString() == "男" ? 0 : 1;
}
/// <summary>
/// 性别转换器2
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return parameter;
}


浙公网安备 33010602011771号