数据绑定(十)Binding的数据转换

当Source端Path所关联的数据与Target端目标属性数据类型不一致时,需要添加数据转换器,数据转换器是一个自定义的类,这个类需要实现IValueConverter接口,这个接口有两个方法需要实现:Convert和ConvertBack,当数据从Source流向Target时,将调用Convert方法,反之,将调用ConvertBack方法

例子,首先定义飞机类型

 

[csharp] view plain copy
 
  1. public enum Category  
  2. {  
  3.     Bomber,  
  4.     Fighter  
  5. }  
  6.   
  7. public enum State  
  8. {  
  9.     Available,  
  10.     Locked,  
  11.     Unknown  
  12. }  
  13.   
  14. public class Plane  
  15. {  
  16.     public Category Category { get; set; }  
  17.     public string Name { get; set; }  
  18.     public State State { get; set; }  
  19. }  


Plane类型的Category将在界面上转换为图片,而State类型将会转换成界面上的CheckBox显示,由于存在两个转换,因此需要提供两个Converter,第一个转换是做Category类型与字符类型的转换,字符串是图片的路径

 

 

[csharp] view plain copy
 
  1. class CategoryToSourceConverter : IValueConverter  
  2. {  
  3.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
  4.     {  
  5.         Category c = (Category)value;  
  6.         switch (c)  
  7.         {  
  8.             case Category.Bomber:  
  9.                 {  
  10.                     return @"\Icons\close.png";  
  11.                 }  
  12.             case Category.Fighter:  
  13.                 {  
  14.                     return @"\Icons\closeing.png";  
  15.                 }  
  16.             default:  
  17.                 {  
  18.                     return null;  
  19.                 }  
  20.         }  
  21.     }  
  22.   
  23.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
  24.     {  
  25.         return new NotImplementedException();  
  26.     }  
  27. }  


由于UI上不能修改图片,所以只实现了从Source到Target的转换

 

另一个转换用于将State数据转换为bool?

 

[csharp] view plain copy
 
  1. public class StateToNullableBoolConverter : IValueConverter  
  2. {  
  3.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
  4.     {  
  5.         State s = (State)value;  
  6.         switch (s)  
  7.         {  
  8.             case State.Locked:  
  9.                 {  
  10.                     return false;  
  11.                 }  
  12.             case State.Available:  
  13.                 {  
  14.                     return true;  
  15.                 }  
  16.             case State.Unknown:  
  17.             default:  
  18.                 {  
  19.                     return null;  
  20.                 }  
  21.         }  
  22.     }  
  23.   
  24.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
  25.     {  
  26.         bool? nb = (bool?)value;  
  27.         switch (nb)  
  28.         {  
  29.             case true:  
  30.                 {  
  31.                     return State.Available;  
  32.                 }  
  33.             case false:  
  34.                 {  
  35.                     return State.Locked;  
  36.                 }  
  37.             case null:  
  38.             default:  
  39.                 {  
  40.                     return State.Unknown;  
  41.                 }  
  42.         }  
  43.     }  
  44. }  


界面代码:

 

 

[html] view plain copy
 
  1. <Window x:Class="WpfApplication1.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:WpfApplication1"  
  5.         Title="MainWindow" Height="275" Width="275">  
  6. <strong>    <Window.Resources>  
  7.         <local:CategoryToSourceConverter x:Key="cts" />  
  8.         <local:StateToNullableBoolConverter x:Key="stnb" />  
  9.     </Window.Resources></strong>  
  10.     <StackPanel Background="LightBlue">  
  11.         <ListBox x:Name="listBoxPlane" Height="160" Margin="5">  
  12.             <ListBox.ItemTemplate>  
  13.                 <DataTemplate>  
  14.                     <StackPanel Orientation="Horizontal">  
  15. <strong>                        <Image Width="20" Height="20" Source="{Binding Path=Category, Converter={StaticResource cts}}" />  
  16.                         <TextBlock Text="{Binding Path=Name}" Width="60" Margin="80,0" />  
  17.                         <CheckBox IsThreeState="True" IsChecked="{Binding Path=State, Converter={StaticResource stnb}}" /></strong>  
  18.                     </StackPanel>  
  19.                 </DataTemplate>  
  20.             </ListBox.ItemTemplate>  
  21.         </ListBox>  
  22.         <Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0" Click="buttonLoad_Click" />  
  23.         <Button x:Name="buttonSave" Content="Save" Height="25" Margin="5,0" Click="buttonSave_Click" />  
  24.     </StackPanel>  
  25. </Window>  


加粗的部分是XAML中对转换器的使用,后台代码中实现了Load和Save两个按钮的点击事件

 

 

[csharp] view plain copy
 
  1. private void buttonLoad_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     List<Plane> planeList = new List<Plane>()  
  4.     {  
  5.         new Plane(){Category=Category.Bomber, Name="B-1", State=State.Unknown},  
  6.         new Plane(){Category=Category.Bomber, Name="B-2", State=State.Unknown},  
  7.         new Plane(){Category=Category.Fighter, Name="F-22", State=State.Unknown},  
  8.         new Plane(){Category=Category.Fighter, Name="Su-47", State=State.Unknown},  
  9.         new Plane(){Category=Category.Bomber, Name="B-52", State=State.Unknown},  
  10.         new Plane(){Category=Category.Fighter, Name="J-10", State=State.Unknown}  
  11.     };  
  12.   
  13.     listBoxPlane.ItemsSource = planeList;  
  14. }  
  15.   
  16. private void buttonSave_Click(object sender, RoutedEventArgs e)  
  17. {  
  18.     StringBuilder sb = new StringBuilder();  
  19.     foreach (Plane p in listBoxPlane.Items)  
  20.     {  
  21.         sb.AppendLine(string.Format("Category={0}, Name={1}, State={2}", p.Category, p.Name, p.State));  
  22.     }  
  23.     File.WriteAllText(@"d:\PlaneList.txt", sb.ToString());  
  24. }  

 

界面效果如图:

当改变checkbox的选中状态时,Plane对象中的值会发生变化

posted on 2017-07-26 09:18  alex5211314  阅读(153)  评论(0编辑  收藏  举报

导航