BindingConverterDemo1

 

<Window x:Class="WpfDemo.BindingConverterDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BindingConverterDemo" Height="300" Width="300"
        xmlns:local="clr-namespace:WpfDemo"
        >
    <Window.Resources>
        <local:CategoryToSourceConverter x:Key="cts"></local:CategoryToSourceConverter>
        <local:StateToNullableBoolConverter x:Key="snbc"></local:StateToNullableBoolConverter>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="listBox" Height="300">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="50" Height="50" Source="{Binding Path=category,Converter={StaticResource cts}}"></Image>
                        <TextBox Width="40" Text="{Binding Path=name}"></TextBox>
                        <CheckBox IsThreeState="True" IsChecked="{Binding Path=state,Converter={StaticResource  ResourceKey=snbc}}"></CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Height="19" Content="load" Click="Button_Click"></Button>
        <Button Height="19" Content="save" Click="Button_Click_1"></Button>
    </StackPanel>
</Window>

  

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace WpfDemo
{
    /// <summary>
    /// BindingConverterDemo.xaml 的交互逻辑
    /// </summary>
    public partial class BindingConverterDemo : Window
    {
        public BindingConverterDemo()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Beauty> list = new List<Beauty>() {
                new Beauty(){ category=Category.御姐, name="西施", state=State.Availabel},
                new Beauty(){ category=Category.御姐, name="貂蝉", state=State.Lock},
                new Beauty(){ category=Category.御姐, name="苍老师", state=State.Unknoww},
                new Beauty(){ category=Category.萝莉, name="罗拉前辈", state=State.Availabel},
                new Beauty(){ category=Category.萝莉, name="小明", state=State.Lock},
                new Beauty(){ category=Category.萝莉, name="杨幂", state=State.Unknoww},
            };
            this.listBox.ItemsSource = list;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach(Beauty b in listBox.Items)
            {
               sb.Append(string.Format("Category:{0},name:{1},State:{2};\n",b.category,b.name,b.state));
            }
            File.WriteAllText("D:\\tt.txt",sb.ToString());
        }
    }

    public enum Category
    {
        御姐,萝莉
    }
    public enum State
    {
        Availabel,Lock,Unknoww
    }
    public class Beauty
    { 
        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, System.Globalization.CultureInfo culture)
        {
            Category c = (Category)value;
            switch(c)
            {
                case Category.御姐:
                    return @"\pic\御姐.jpg";
                case Category.萝莉:
                    return @"\pic\萝莉.jpg";
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class StateToNullableBoolConverter:IValueConverter
    {
        //從Source流向Target時
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            State s = (State)value;
            switch(s)
            {
                case State.Availabel:
                    return true;
                case State.Lock:
                    return false;
                case State.Unknoww:
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool? b = (bool?)value;
            switch(b)
            {
                case true:
                    return State.Availabel;
                case false:
                    return State.Lock;
                case null:
                default:
                    return State.Unknoww;
            }
        }
    }
}

  

 

posted @ 2021-07-29 16:46  zq爱生活爱代码  阅读(17)  评论(0编辑  收藏  举报