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下载了
点击这里下载源码

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