wpf之学习一(IValueConverter)

学习IValueConverter的使用

StatuToNullableBoolConverter
 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; }
    }

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值