WPF CheckBox綁定枚舉數據源(數據列表)

項目背景:我需要描述一個人的八種不同的行爲,每一種行爲都是獨立的。現在將通過八個CheckBox去分別描述這八種行爲,勾選上則存在這種行爲,不存在則不勾選,界面圖如下:

在這裏插入圖片描述
界面代碼:

<Window x:Class="CheckBoxConverterDemo.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:CheckBoxConverterDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:ValueConverter x:Key="contentColor"/>
    </Window.Resources>
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <CheckBox Grid.Row="0" Margin="10,0,0,0" Grid.Column="0" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Eatting}" Content="喫飯"></CheckBox>
            <CheckBox Grid.Row="0" Grid.Column="1" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Dringking}" Content="喝茶"></CheckBox>
            <CheckBox Grid.Row="0" Grid.Column="2" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Sleeping}" Content="睡覺"></CheckBox>
            <CheckBox Grid.Row="0" Grid.Column="3" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Playing}" Content="打遊戲"></CheckBox>
            <CheckBox Grid.Row="1" Margin="10,0,0,0" Grid.Column="0" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Speaking}" Content="說話"></CheckBox>
            <CheckBox Grid.Row="1" Grid.Column="1" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Smiling}" Content="微笑"></CheckBox>
            <CheckBox Grid.Row="1" Grid.Column="2" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Crying}" Content="哭泣"></CheckBox>
            <CheckBox Grid.Row="1" Grid.Column="3" IsChecked="{Binding Path=Person.State,Converter={StaticResource contentColor},ConverterParameter=Lazing}" Content="發呆"></CheckBox>
        </Grid>
        <Grid Grid.Row="2">
            <Button Width="80" Height="35" Content="確定" Command="{Binding PressOkCommand}"></Button>
        </Grid>
    </Grid>
</Window>

一個人的這八種行爲,通過一個枚舉類來表示:

using System;

namespace CheckBoxConverterDemo
{
    [Flags]
    public enum TypeEnum
    {
        None = 0x0,
        Eatting = 0x1,
        Dringking = 0x2,
        Sleeping = 0x4,
        Playing = 0x8,
        Speaking = 0x10,
        Smiling = 0x20,
        Crying = 0x40,
        Lazing = 0x80,
    }

    public class Person
    {
        public TypeEnum State { get; set; }
    }
}

然後通過轉換器去轉換不同的狀態勾選不同的CheckBox

using System;
using System.Windows.Data;

namespace CheckBoxConverterDemo
{
    /// <summary>
    /// 自定義事件轉換
    /// </summary>
    //[ValueConversion(typeof(TypeEnum), typeof(bool))]
    public class ValueConverter : IValueConverter
    {
        public TypeEnum SetValue { get; set; }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return false;

            var getData = (TypeEnum)value;
            SetValue = getData;
            switch (parameter.ToString())
            {
                case "Eatting":
                    if ((getData & TypeEnum.Eatting) == TypeEnum.Eatting)
                        return true;
                    break;
                case "Dringking":
                    if ((getData & TypeEnum.Dringking) == TypeEnum.Dringking)
                        return true;
                    break;
                case "Sleeping":
                    if ((getData & TypeEnum.Sleeping) == TypeEnum.Sleeping)
                        return true;
                    break;
                case "Playing":
                    if ((getData & TypeEnum.Playing) == TypeEnum.Playing)
                        return true;
                    break;
                case "Speaking":
                    if ((getData & TypeEnum.Speaking) == TypeEnum.Speaking)
                        return true;
                    break;
                case "Smiling":
                    if ((getData & TypeEnum.Smiling) == TypeEnum.Smiling)
                        return true;
                    break;
                case "Crying":
                    if ((getData & TypeEnum.Crying) == TypeEnum.Crying)
                        return true;
                    break;
                case "Lazing":
                    if ((getData & TypeEnum.Lazing) == TypeEnum.Lazing)
                        return true;
                    break;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //SetValue = TypeEnum.None;
            if ((bool)value)
            {
                switch (parameter.ToString())
                {
                    case "Eatting":
                        SetValue = SetValue | TypeEnum.Eatting;
                        break;
                    case "Dringking":
                        SetValue = SetValue | TypeEnum.Dringking;
                        break;
                    case "Sleeping":
                        SetValue = SetValue | TypeEnum.Sleeping;
                        break;
                    case "Playing":
                        SetValue = SetValue | TypeEnum.Playing;
                        break;
                    case "Speaking":
                        SetValue = SetValue | TypeEnum.Speaking;
                        break;
                    case "Smiling":
                        SetValue = SetValue | TypeEnum.Smiling;
                        break;
                    case "Crying":
                        SetValue = SetValue | TypeEnum.Crying;
                        break;
                    case "Lazing":
                        SetValue = SetValue | TypeEnum.Lazing;
                        break;
                }
            }
            else
            {
                switch (parameter.ToString())
                {
                    case "Eatting":
                        if ((SetValue & TypeEnum.Eatting) == TypeEnum.Eatting)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Eatting;
                        break;
                    case "Dringking":
                        if ((SetValue & TypeEnum.Dringking) == TypeEnum.Dringking)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Dringking;
                        break;
                    case "Sleeping":
                        if ((SetValue & TypeEnum.Sleeping) == TypeEnum.Sleeping)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Sleeping;
                        break;
                    case "Playing":
                        if ((SetValue & TypeEnum.Playing) == TypeEnum.Playing)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Playing;
                        break;
                    case "Speaking":
                        if ((SetValue & TypeEnum.Speaking) == TypeEnum.Speaking)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Speaking;
                        break;
                    case "Smiling":
                        if ((SetValue & TypeEnum.Smiling) == TypeEnum.Smiling)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Smiling;
                        break;
                    case "Crying":
                        if ((SetValue & TypeEnum.Crying) == TypeEnum.Crying)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Crying;
                        break;
                    case "Lazing":
                        if ((SetValue & TypeEnum.Lazing) == TypeEnum.Lazing)  //如果存在則移除
                            SetValue = SetValue ^ TypeEnum.Lazing;
                        break;
                }
            }
            return SetValue;
        }
    }
}

在ViewModel內進行數據綁定:

using System.ComponentModel;
using System.Windows.Input;

namespace CheckBoxConverterDemo
{
    public class MainWindowViewModel
    {
        public MainWindowViewModel()
        {
            InitData();
        }
        private Person _person;

        public Person Person
        {
            get
            { return _person; }

            set
            {
                _person = value;
                RaisePropertyChanged("Person");
            }
        }

        public ICommand PressOkCommand { get { return new RelayCommand(PressOK, CanPreess); } }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void InitData()
        {
            Person = new Person();
            
            //這個是你傳入的設定的初始值,比如我開始設置這個人有這三種狀態,那麼界面就勾選了這三個狀態對應的CheckBox,界面在一開始已經給出了
            Person.State = TypeEnum.Dringking | TypeEnum.Smiling | TypeEnum.Playing;  
        }

        private bool CanPreess()
        {
            return true;
        }

        private void PressOK()
        {
            TypeEnum getValue = Person.State;  //這裏就是你點擊確定後,界面你勾選的返回的當前編輯的這個人的狀態
        }
    }
}

源碼:放在CSDN下載了
點擊這裏下載源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章