学习IValueConverter的使用

 public class StatuToNullableBoolConverter : IValueConverter
{ /// <summary>
/// 将Statu转换为bool?
/// </summary>
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;
}
} /// <summary>
/// 将bool?转换为Statu
/// </summary>
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;
default:
return State.Unknown;
}
}
}
 
CategoryToSourceConverter

 
class CategoryToSourceConverter : IValueConverter
{ public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Category c = (Category)value;
switch (c)
{
case Category.Bomber:
return @"\Icon\sys_ct_icon\chardevice.ico";
case Category.Fighter:
return @"\Icon\sys_ct_icon\file_broken.ico";
default:
return null;
}
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
 
WindowConvertListItem xaml文件

 
<Window x:Class="WpfApplication1.WindowConvertListItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1.DXSL.BLL"
Title="WindowConvertListItem" Height="300" Width="300">
<Window.Resources>
<local:CategoryToSourceConverter x:Key="cts" />
<local:StatuToNullableBoolConverter x:Key="ctnb" />
</Window.Resources> <Grid>
<StackPanel Background="LightBlue">
<ListBox x:Name="listboxPlane" Height="160" Margin="5,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= Statu, Converter={StaticResource ctnb}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="btnLoad" Content="load" Height="25" Margin="5,5" Click="btnLoad_Click" />
<Button x:Name="btnSave" Content="save" Height="25" Margin="5,5" Click="btnSave_Click" />
</StackPanel>
</Grid>
</Window>
 
WindowConvertListItem cs文件

 
/// <summary>
/// WindowConvertListItem.xaml 的交互逻辑
/// </summary>
public partial class WindowConvertListItem : Window
{
public WindowConvertListItem()
{
InitializeComponent();
} private void btnLoad_Click(object sender, RoutedEventArgs e)
{
List<Plane> list = new List<Plane>()
{
new Plane (){Category =Category.Bomber,Name="b_1", State =State.Unknown},
new Plane (){Category =Category.Bomber,Name="b_2",State =State.Available},
new Plane (){Category =Category.Fighter,Name="b_3",State =State.Locked},
new Plane (){Category =Category.Fighter,Name="b_4", State =State.Available}
}; this.listboxPlane.ItemsSource = list;
} private void btnSave_Click(object sender, RoutedEventArgs e)
{
StringBuilder str = new StringBuilder(); foreach (Plane item in this.listboxPlane.Items)
{
str.AppendLine(string.Format("{0},{1},{2}", item.Category, item.Name, item.State));
} System.IO.File.WriteAllText("plane.csv", str.ToString());
}
}
class Plane

 
 /// <summary>
/// 种类
/// </summary>
public enum Category
{
Bomber,
Fighter
} /// <summary>
/// 状态
/// </summary>
public enum State
{
Available,
Locked,
Unknown
} public class Plane
{
public Category Category { get; set; } public string Name { get; set; } public State State { get; set; }
}

转载:http://www.cnblogs.com/-ShiL/archive/2013/03/14/Star201303141456.html

最新文章

  1. XML语言基础2 DTD
  2. Bootstrap~Panel和Table
  3. 来吧!带你玩转 Excel VBA
  4. 【编程题目】在 O(1)时间内删除链表结点
  5. HTML第二节课
  6. TForm的显示过程
  7. HDU 4627 The Unsolvable Problem(简单题)
  8. C语言基础学习运算符-基本算术运算符
  9. Nginx 变量漫谈(八)
  10. c语言 数组最小数
  11. Dictionary导致CPU暴涨
  12. Android Studio配置统一管理依赖版本号引用
  13. javascript日期格式yyyyMMddHHmmss
  14. shapefile的使用和地理信息的获得
  15. Java:Linux上java -jar xxx.jar&amp;java -cp 区别
  16. Matplotlib用法
  17. Java并发—原子类,java.util.concurrent.atomic包(转载)
  18. Android系统移植与调试之------->如何使用PhotoShop转换24位的bmp图片为16位bmp图片
  19. poj 1947 树形背包
  20. HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015年百度之星程序设计大赛 - 初赛(1)

热门文章

  1. C++成员指针
  2. img src 直接显示图片字符串,微信例子
  3. Win7常用但是被忽略的快捷键
  4. java部分基础知识整理----百度脑图版
  5. [Xcode 实际操作]四、常用控件-(9)普通警告窗口的使用
  6. 通过jQuery实现AJAX
  7. Android Gradle 学习笔记(六):Gradle 插件
  8. vue2格式化时间戳
  9. 默认约束 default
  10. Testing Round #12 B