Sam*_*Sam 400 data-binding wpf enums radio-button
我有这样的枚举:
public enum MyLovelyEnum
{
FirstSelection,
TheOtherSelection,
YetAnotherOne
};
Run Code Online (Sandbox Code Playgroud)
我的DataContext中有一个属性:
public MyLovelyEnum VeryLovelyEnum { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的WPF客户端中有三个RadioButton.
<RadioButton Margin="3">First Selection</RadioButton>
<RadioButton Margin="3">The Other Selection</RadioButton>
<RadioButton Margin="3">Yet Another one</RadioButton>
Run Code Online (Sandbox Code Playgroud)
现在如何将RadioButtons绑定到属性以进行正确的双向绑定?
Sco*_*ott 550
您可以进一步简化已接受的答案.您可以显式传入枚举值而不是字符串表示形式,而不是在xaml中将枚举输入为xaml中的字符串并在转换器中执行更多工作,而在CrimsonX注释时,错误会在编译时而不是运行时抛出:
<StackPanel>
<StackPanel.Resources>
<local:ComparisonConverter x:Key="ComparisonConverter" />
</StackPanel.Resources>
<RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:YourEnumType.Enum1}}" />
<RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:YourEnumType.Enum2}}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
然后简化转换器:
public class ComparisonConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
Run Code Online (Sandbox Code Playgroud)
IsChecked
是一个可空类型,所以返回Nullable<Boolean>
似乎是一个合理的解决方案.
ConverterParameter = {x:静态local:YourClass + YourNestedEnumType.Enum1}
但是,由于此Microsoft Connect问题,VS2010中的设计器将不再加载说明"Type 'local:YourClass+YourNestedEnumType' was not found."
,但该项目确实可以成功编译和运行.当然,如果能够直接将枚举类型移动到命名空间,则可以避免此问题.
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((Enum)value).HasFlag((Enum)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
Run Code Online (Sandbox Code Playgroud)
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) {
return false; // or return parameter.Equals(YourEnumType.SomeDefaultValue);
}
return value.Equals(parameter);
}
Run Code Online (Sandbox Code Playgroud)
Lar*_*ars 381
您可以使用更通用的转换器
public class EnumBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
if (Enum.IsDefined(value.GetType(), value) == false)
return DependencyProperty.UnsetValue;
object parameterValue = Enum.Parse(value.GetType(), parameterString);
return parameterValue.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
return Enum.Parse(targetType, parameterString);
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
在XAML-Part中你使用:
<Grid>
<Grid.Resources>
<l:EnumBooleanConverter x:Key="enumBooleanConverter" />
</Grid.Resources>
<StackPanel >
<RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=FirstSelection}">first selection</RadioButton>
<RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=TheOtherSelection}">the other selection</RadioButton>
<RadioButton IsChecked="{Binding Path=VeryLovelyEnum, Converter={StaticResource enumBooleanConverter}, ConverterParameter=YetAnotherOne}">yet another one</RadioButton>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
小智 25
对于EnumToBooleanConverter答案:在没有返回DependencyProperty.UnsetValue的情况下,考虑在单选按钮IsChecked值变为false的情况下返回Binding.DoNothing.前者表示存在问题(并且可能向用户显示红色矩形或类似的验证指示符),而后者仅表示不应该执行任何操作,这是在这种情况下需要的.
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convertback.aspx http://msdn.microsoft.com/en-us/library/system.windows.data.binding .donothing.aspx
我将在ListBox中使用RadioButtons,然后绑定到SelectedValue.
这是关于这个主题的旧帖子,但基本思想应该是相同的:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/323d067a-efef-4c9f-8d99-fecf45522395/
归档时间: |
|
查看次数: |
128062 次 |
最近记录: |