一、說明
很多時候,我們要把一個枚舉的屬性的綁定到一組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了。
因為代碼沒有多少,就不上傳源碼了。
本文原創