2012-12-20 54 views
0

我有時會厭倦爲MVVM UI上的每個單選按鈕創建單獨的IsChecked * SomePropertyName *。另一種方法是爲每個按鈕命名並找到IsChecked = true的那個,然後在我的強文本模型中將其名稱翻譯爲有意義的內容。Silverlight/WPF - RadioButton.IsChecked綁定到智能布爾源

如果有一種方法支持到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)); 
} 

回答

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 
} 

希望這會有所幫助。聖誕快樂,上帝保佑。

  • 拉沙德