E不小心

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、说明

  很多时候,我们要把一个枚举的属性的绑定到一组RadioButton上。大家都知道是使用IValueConverter来做,但到底怎么做才好?

  而且多个RadioButton的Checked和UnChecked都会触发绑定,这样就会调多次的Set。

二、目的

  实现一个枚举属性绑定到多个RadioButton, 属性的Set方法不会被触发多次。

三、实现

  方法大家都知道,就是利用Converter和ConevertParamter属性。

  因为多个控件绑定一个属性,Checked和UnChecked之间属性可是会进行多次的Set的。

  这就得用到Binding.DoNothing 了,详细的说明:https://msdn.microsoft.com/zh-cn/library/system.windows.data.binding.donothing(v=vs.110).aspx

四、源码

  源码就很简单了。

using System;
using System.Globalization;
using System.Windows.Data;

namespace BindingEnumToRadioButton
{
    public class EnumToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? false : value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }
}

五、例子

using System.ComponentModel;
using System.Windows;

namespace BindingEnumToRadioButton
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new SampleClass();
        }
    }

    public enum SampleEnum
    {
        One,
        Two,
        Three
    }

    public class SampleClass : INotifyPropertyChanged
    {
        private SampleEnum _sampleEnum;

        public SampleEnum SampleEnum
        {
            get { return _sampleEnum; }
            set
            {
                _sampleEnum = value;
                HitCount++;
            }
        }

        //为了显示Set的触发次数
        private int _hitCount;
        public int HitCount
        {
            get { return _hitCount; }
            set
            {
                _hitCount = value;
                OnPropertyChanged("HitCount");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string p_propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p_propertyName));
            }
        }
    }
}

在测试类中加入了HitCount属性,来显示Set的执行次数。每经过一次Set方法,都会加1.

Xaml:

<Window x:Class="BindingEnumToRadioButton.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:BindingEnumToRadioButton"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="350"
        Width="525">
    <Window.Resources>
        <local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />
    </Window.Resources>
    <Grid>
        <StackPanel>
            <RadioButton Content="RadioButton One"
                         IsChecked="{Binding Path=SampleEnum,Converter={StaticResource EnumToBooleanConverter},ConverterParameter={x:Static local:SampleEnum.One}}" />
            <RadioButton Content="RadioButton Two"
                         IsChecked="{Binding Path=SampleEnum,Converter={StaticResource EnumToBooleanConverter},ConverterParameter={x:Static local:SampleEnum.Two}}" />
            <RadioButton Content="RadioButton Three"
                         IsChecked="{Binding Path=SampleEnum,Converter={StaticResource EnumToBooleanConverter},ConverterParameter={x:Static local:SampleEnum.Three}}" />
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Value: " />
                <TextBlock Text="{Binding SampleEnum}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Hit Count: " />
                <TextBlock Text="{Binding HitCount}" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

六、效果

 

目的达到了,枚举的属性值是正常的变化,Set方法的触发次数同样是正常的。

七、总结

这个东西很简单,但很实用,方法大家也都能想到,最多就是差到没想到用Binding.DoNothing了。

因为代码没有多少,就不上传源码了。

 

本文原创

转载请注明出处:http://www.cnblogs.com/gaoshang212/p/4973300.html

posted on 2015-11-18 00:08  E不小心  阅读(4004)  评论(4编辑  收藏  举报