wpf ValueConverter的经典使用

 

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="263.927" Width="354.595"  SizeToContent ="Height">
    <Window.Resources>
        <local:CategoryToSourceConverter x:Key="CategoryToSourceConverter"></local:CategoryToSourceConverter>
        <local:StateToNullableBoolConverter x:Key="StateToNullableBoolConverter"></local:StateToNullableBoolConverter>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <ListBox x:Name="lst" Margin="5">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Path=Category,Converter={StaticResource CategoryToSourceConverter}}" Margin="30"/>
                            <TextBlock Text="{Binding Path=Name}" Margin="30" HorizontalAlignment="Center"/>
                            <CheckBox IsChecked="{Binding Path=State,Converter={StaticResource StateToNullableBoolConverter}}" Margin="30" HorizontalAlignment="Right"/>
                        </StackPanel>
                        
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button x:Name="btnLoad" Margin="5" Content="Load" Click="BtnLoad_Click"></Button>
            <Button x:Name="btnSave" Margin="5" Content="Save" Click="BtnSave_Click"/>

        </StackPanel>
        
    </Grid>
    
</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.Navigation;
using System.Windows.Shapes;

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

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach(Plane p in lst.Items)
            {
                sb.AppendLine(string.Format("Category={0},Name={1},State={2}", p.Category, p.Name, p.State));
            }
            File.WriteAllText("plane.txt", sb.ToString());
        }

        private void BtnLoad_Click(object sender, RoutedEventArgs e)
        {
            List<Plane> planes = new List<Plane>()
            {
                new Plane(){ Category=Category.Bomber, Name="B1", State= State.Available},
                new Plane(){ Category=Category.Fighter, Name="F1", State= State.Locked},
                new Plane(){ Category=Category.Bomber, Name="B2", State= State.Unknown},
                new Plane(){ Category=Category.Fighter, Name="F2", State= State.Available},
                new Plane(){ Category=Category.Bomber, Name="B3", State= State.Locked}
            };
            lst.ItemsSource = planes;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WpfApp2
{
    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 @"\Images\1.ico";
                case Category.Fighter:
                    return @"\Images\2.ico";
                default:
                    return null;
            }
        }

        //不会被调用
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

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

            }

        }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值