WPF应用Binding之数据转换

本文介绍了在WPF中如何使用数据转换来处理数据源与目标类型不一致的情况,例如将车的类型转化为对应的图片路径,以及将车的运行状态转化为CheckBox的状态。通过实例详细解析了类定义和XAML的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时数据源的类型与目标类型不一致时,需要把源通过一定的转换之后才能绑定到目标之上。

本例:

(1) 把车的类型转换成车所对应的图片路径;

(2) 把车的运行状态转换成CheckBox的状态;


1、类/类型定义

    public enum Category//车的类型
    {
        Car,
        Bus,
    }

    public enum State//车的状态
    {
        Running,
        Stop,
        Unknow,
    }

    public class Vehicle
    {
        public string Name { get; set; }
        public Category Category { get; set; }
        public State State { get; set; }
    }

2、车的类型转换类

    public class CategoryToPictureConverter : IValueConverter
    {
        /* 数据从Source到Targe时,Convert被调用 */
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Category category = (Category)value;
            switch (category)
            {
                case Category.Car:
                    return @"\Icons\car.jpg";
                case Category.Bus:
                    return @"\Icons\bus.jpg";
                default:
                    break;
            }

            return null;
        }

        /* 
         * 数据从Targe到Source时,ConvertBack被调用
         * 目前不会被调用
         */
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

3、车的状态转换类

    public class StateToNullableBoolConverter : IValueConverter
    {
        /* 将State转换为bool? */
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            State state = (State)value;
            switch (state)
            {
                case State.Running:
                    return true;
                case State.Stop:
                    return false;
                case State.Unknow:
                default:
                    break;
            }

            return null;
        }

        /* 将bool?转换为State */
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool? b = (bool?)value;
            if (null == b)
            {
                return State.Unknow;
            }

            switch (b)
            {
                case true:
                    return State.Running;
                case false:
                    return State.Stop;
                default:
                    break;
            }

            return State.Unknow;
        }
    }


4、XAML

<Window.Resources>
        <local:CategoryToPictureConverter x:Key="c2pc"/>
        <local:StateToNullableBoolConverter x:Key="s2bc"/>
    </Window.Resources>

    <Grid>
        <ListBox x:Name="ListBoxVehicle" ScrollViewer.VerticalScrollBarVisibility="Auto" VerticalAlignment="Center" Margin="5">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="80" Height="60" Source="{Binding Path=Category, Converter={StaticResource c2pc}}" VerticalAlignment="Center"/>
                        <TextBlock Text="{Binding Path=Name}" MinWidth="60" VerticalAlignment="Center" Margin="5"/>
                        <CheckBox IsThreeState="True" IsChecked="{Binding Path=State, Converter={StaticResource s2bc}}" VerticalAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

5、C#数据初始化

        public MainWindow()
        {
            InitializeComponent();

            LoadDatas();
        }

        private void LoadDatas()
        {
            List<Vehicle> vehicleList = new List<Vehicle>()
            {
                new Vehicle(){Category = Category.Car, Name="Audo A4", State = State.Unknow},
                new Vehicle(){Category = Category.Car, Name="Audo A4 L", State = State.Unknow},
                new Vehicle(){Category = Category.Car, Name="Audo A6", State = State.Unknow},
                new Vehicle(){Category = Category.Car, Name="Audo A6 L", State = State.Unknow},
                new Vehicle(){Category = Category.Bus, Name="金龙客车A1", State = State.Unknow},
                new Vehicle(){Category = Category.Bus, Name="金龙客车A2", State = State.Unknow},
                new Vehicle(){Category = Category.Bus, Name="宇通客车X1", State = State.Unknow},
                new Vehicle(){Category = Category.Bus, Name="宇通客车X2", State = State.Unknow},
            };
            ListBoxVehicle.ItemsSource = vehicleList;
        }


6、图片




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值