WPF应用Binding之数据转换

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

本例:

(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、图片




没有更多推荐了,返回首页