1. 程式人生 > >WPF應用Binding之資料轉換

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、圖片