Yog*_*esh 3 c# wpf xaml converter
我已经通过这个如何将RadioButtons绑定到枚举?
并接受这个问题的答案包含使用泛型Enum到布尔转换器.
我的问题是我在View中有两个单选按钮和一个枚举
public Enum LinkType
{
A,
B,
C,
D,
E,
F
}
Run Code Online (Sandbox Code Playgroud)
在ViewModel中,我有一个Called属性
public LinkType MyLinktype
{
get;set;
}
Run Code Online (Sandbox Code Playgroud)
如果ViewModel中enum的属性具有A,C,E中的值,则第一个单选按钮可以为true,如果ViewModel中的枚举属性具有值,则第二个单选按钮可以为true.B,d,F
那么,如何在通用EnumTo布尔转换器中的转换器参数中传递多个值,如下所示
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);
}
Run Code Online (Sandbox Code Playgroud)
那么如果我想在XAML中使用这样的东西,我必须在转换器中做出哪些改变
<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter=A,C,F}">Odd LinkType</RadioButton>
<RadioButton IsChecked="{Binding Path=Mylinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter=B,D,E}">Even Link Type</RadioButton>
Run Code Online (Sandbox Code Playgroud)
您可以在xaml中定义一个数组:
<x:Array Type="LinkType" x:Key="ar">
<LinkType>A</LinkType>
<LinkType>B</LinkType>
</x:Array>
Run Code Online (Sandbox Code Playgroud)
然后将其作为参数传递
<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter={StaticResource ar}}">Odd LinkType</RadioButton>
Run Code Online (Sandbox Code Playgroud)
您必须修复转换器,以便正确处理数组作为转换器参数.
归档时间: |
|
查看次数: |
11831 次 |
最近记录: |