zoukankan      html  css  js  c++  java
  • wpf学习之(IValueConverter)

     

    学习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; } } }
     
     
    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 @"Iconsys_ct_iconchardevice.ico";
                    case Category.Fighter:
                        return @"Iconsys_ct_iconfile_broken.ico";
                    default:
                        return null;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
     
     
    <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>
     
     
    /// <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());
            }
        }
     
     /// <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

  • 相关阅读:
    CodeForces 7B
    CodeForces 4D
    离散化
    线段树入门
    洛谷 P3951 小凯的疑惑(赛瓦维斯特定理)
    Codeforces 1295D Same GCDs (欧拉函数)
    Codeforces 1295C Obtain The String (二分)
    Codeforces 1295B Infinite Prefixes
    Codeforces 1295A Display The Number(思维)
    Codeforces 1294F Three Paths on a Tree(树的直径,思维)
  • 原文地址:https://www.cnblogs.com/candyzhmm/p/7080327.html
Copyright © 2011-2022 走看看