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));
}