Silverlight/WPF - RadioButton.IsChecked绑定到一个智能布尔源



我有时会厌倦为MVVM UI上的每个单选按钮创建单独的IsChecked*SomePropertyName*。另一种方法是命名每个按钮,并找到IsChecked=true的按钮,然后将其名称转换为我的强文本模型中有意义的内容。

如果在Silverlight和/或WPF中有一个集合来封装所有这些复杂的逻辑,那就太好了。我的代码中的一个示例用例是:

<Page x:Name="idHost"
      ...>
<TextBlock Text="{Binding Path=RadioButtonSource.CurrentEnabledButton, Mode=OneWay, StringFormat='Selected Filter: {0}', TargetNullValue='Selected Filter: not selected', ElementName=idHostPage}" />
<RadioButton IsChecked="{Binding Path=RadioButtonSource[Inherited], Mode=TwoWay, ElementName=idHostPage}"
             IsThreeState="False"
             GroupName="PostFilter"
             Content="Inherited" />
<RadioButton IsChecked="{Binding Path=RadioButtonSource[Direct], Mode=TwoWay, ElementName=idHostPage}"
             IsThreeState="False"
             GroupName="PostFilter"
             Content="Direct" />
...

页面的代码如下:

public partial class MyPage : Page {
    public MyPage() {
        this.RadioButtonSource = new RadioButtonSource();
    }
    public RadioButtonSource RadioButtonSource {
        get { return (RadioButtonSource)GetValue(RadioButtonSourceProperty); }
        set { SetValue(RadioButtonSourceProperty, value); }
    }
    public static readonly DependencyProperty RadioButtonSourceProperty = DependencyProperty.Register("RadioButtonSource", typeof(RadioButtonSource), typeof(MyPage), new PropertyMetadata(null));
}

这是我已经开发的问题的解决方案。将所有这些繁忙的工作封装到一个可重用的类中。

public class RadioButtonSource : INotifyPropertyChanged {
    #region Member Field(s)
    Dictionary<string, bool> m_RadioButtonFlags;
    #endregion
    #region Event(s)
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
    #region Con/Destructor(s)
    public RadioButtonSource() {
        this.m_RadioButtonFlags = new Dictionary<string, bool>();
    }
    #endregion
    #region Exposed Proper(y|ies)
    public string CurrentEnabledButton {
        get {
            var q = from key in this.m_RadioButtonFlags.Keys
                    where this.m_RadioButtonFlags[key]
                    select key;
            return q.FirstOrDefault();
        }
    }
    [IndexerName("Item")]
    public bool this[string radioButtonName] {
        get {
            if (string.IsNullOrEmpty(radioButtonName))
                throw new ArgumentNullException("radioButtonName");
            if (this.m_RadioButtonFlags.ContainsKey(radioButtonName))
                return this.m_RadioButtonFlags[radioButtonName];
            var returnValue = false;
            this.m_RadioButtonFlags.Add(radioButtonName, returnValue);
            return returnValue;
        }
        set {
            if (string.IsNullOrEmpty(radioButtonName))
                throw new ArgumentNullException("radioButtonName");
            if (this.CurrentEnabledButton == radioButtonName)
                return;
            this._ChangeFlags(radioButtonName);
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Item[]"));
                this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentEnabledButton"));
            }
        }
    }
    #endregion
    #region Method(s)
    void _ChangeFlags(string radioButtonName) {
        if (string.IsNullOrEmpty(radioButtonName))
            throw new ArgumentNullException("radioButtonName");
        if (!this.m_RadioButtonFlags.ContainsKey(radioButtonName))
            this.m_RadioButtonFlags.Add(radioButtonName, true);
        foreach (var key in this.m_RadioButtonFlags.Keys.ToArray()) {
            if (key != radioButtonName)
                this.m_RadioButtonFlags[key] = false;
            else
                this.m_RadioButtonFlags[key] = true;
        }
    }
    #endregion
}

相关内容

最新更新