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

时间:2012-12-20 16:40:40

标签: wpf silverlight mvvm radio-button

我有时会厌倦为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));
}

1 个答案:

答案 0 :(得分:0)

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

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
}

希望这会有所帮助。圣诞快乐,上帝保佑。

  • 拉沙德