WPF(Converter 练习[很详细])

<Window x:Class="TestOfDataConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestOfDataConverter"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CategoryToSourceConverter x:Key="cts" />
        <local:StateToNullableBoolConverter x:Key="stnb" />    
    </Window.Resources>
    <StackPanel Background="LightBlue" >
        <ListBox x:Name="listBoxPlane" 
                 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="buttonSave" 
                Content="Save"
                Height="25"
                Margin="5,5"
                Click="buttonSave_Click" />
    </StackPanel>
    
</Window>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestOfDataConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonLoad_Click(object sender, RoutedEventArgs e)
        {
            List<Plane> planeList = new List<Plane>()
            {
                  new Plane(){Category = Category.Bomber,Name = "B-1",State = State.Unknown},
                  new Plane(){Category = Category.Bomber,Name = "B-2",State = State.Unknown}, 
                  new Plane(){Category = Category.Fighter,Name = "F-22",State = State.Unknown}, 
                  new Plane(){Category = Category.Fighter,Name = "Su-47",State = State.Unknown}, 
                  new Plane(){Category = Category.Bomber,Name = "B-52",State = State.Unknown}, 
                  new Plane(){Category = Category.Bomber,Name = "j-10",State = State.Unknown}                       
            };

            this.listBoxPlane.ItemsSource = planeList;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Plane p in listBoxPlane.Items)
            {
                sb.AppendLine(string.Format("Category={0},Name={1},State={2}", p.Category, p.Name, p.State));
            }

            File.WriteAllText(@"PlaneList.txt",sb.ToString());
        }
    }

    public enum Category
    {
        Bomber, Fighter
    }

    public enum State
    {
        Available, Locked, Unknown
    }

    public class Plane
    {
        public Category Category { get; set; }

        public string Name { get; set; }

        public State State { get; set; }
    }

    public class CategoryToSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Category c = (Category)value;

            switch (c)
            {
                case Category.Bomber:
                    return @"Icons\Bomber.jpg";
                case Category.Fighter:
                    return @"Icons\Fighter.jpg";
                default:
                    return null;
            }
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class StateToNullableBoolConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            State s = (State)value;
            switch (s)
            {
                case State.Locked:
                    return false;
                case State.Available:
                    return true;
                case State.Unknown:
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? nb = (bool?) value;
            switch (nb)
            {
                case true:
                    return State.Available;
                case false:
                    return State.Locked;
                case null:
                default:
                    return State.Unknown;
            }
        }
    }
}


原文地址:https://www.cnblogs.com/wjchang/p/3671536.html