当Source端Path所关联的数据与Target端目标属性数据类型不一致时,添加数据转换器(Data Converter),
简单的数据转换(如double转string)WPF类库会自动转换。
手动转换时需要手动写Converter,创建一个类,并实现IValueConverter接口。
public interface IValueConverter
{
object Convert(object value, Type targetType, object parameter, CultureInfo culture);//当数据从Binding的Source流向Target时调用
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);//反之调用ConvertBack
}
例:
用于在UI上显示图片和将Source的三个值映射到CheckBox控件的IsChecked属性值(bool?类型)
//种类,用于显示图片
public enum Category
{
Bomber,
Fighter
}
//状态 CheckBox对应的三种状态
public enum State
{
Available,
Locked,
Unknown
}
//飞机类
public class Plane
{
public Category Category { set; get; }
public string Name { set; get; }
public State State { set; get; }
}
提供两个Converter用于转换数据,一个将Category单项转为string(XAML自动解析为图片),另一个实现State与bool?的双向转换。
public class CategoryToSourceConverter :IValueConverter
{
//将Category转换为Uri
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Category c = (Category)value;
switch (c)
{
case Category.Bomber:
return @"\Icons\Bomber.png";//数据在项目Icons目录里
case Category.Fighter:
return @"\Icons\Fighter.png";
default:
return null;
}
}
//不被调用
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
//将Stae转换为bool?
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
State s = (State)value;
switch (s)
{
case State.Available:
return true;
case State.Locked:
return false;
case State.Unknown:
default:
return null;
}
}
//将bool?转换为State
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? nb = (bool?)value;
switch (nb)
{
case true:
return State.Available;
case false:
return State.Locked;
case null:
default:
return State.Unknown;
}
}
之后再XAML里消费这些Converter
<Window x:Class="Windows6.Window6_4_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Windows6.BLL"
Title="Window6_4_2" Height="266" Width="300">
<Window.Resources>
<local:CategoryToSourceConverter x:Key="cts"/>
<local:StateToNullableBollConverter x:Key="stnb"/>
Window.Resources>
<StackPanel Background="LightBlue">
<ListBox x:Name="listBoxPlan" Height="160" Margin="5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="20" Height="20"
Source="{Binding Path=Category, Converter={StaticResource cts}}"/>
<TextBlock Text="{Binding Path=Name}" Width="60" Margin="80,0"/>
<CheckBox IsThreeState="True"
IsChecked="{Binding Path=State, Converter={StaticResource stnb}}"/>
StackPanel>
DataTemplate>
ListBox.ItemTemplate>
ListBox>
<Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0"
Click="buttonLoad_Click"/>
<Button x:Name="btnSave" Content="Save" Height="25" Margin="5,5"
Click="btnSave_Click"/>
StackPanel>
Window>
最后实现两个按钮的点击事件
private void buttonLoad_Click(object sender, RoutedEventArgs e)
{
List planeList = new List()
{
new Plane() {Category = Category.Bomber, Name = "B-1", State = State.Unknown},
new Plane() {Category = Category.Fighter, Name = "F-22", State = State.Unknown}
};
this.listBoxPlan.ItemsSource = planeList;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (Plane p in listBoxPlan.Items)
{
sb.AppendLine(string.Format("Category={0},Name={1},State={2}", p.Category, p.Name, p.State));
}
File.WriteAllText(@"D:\test\PlaneList.txt", sb.ToString());
}