c# – 如何将通用枚举中的多个转换器参数传递给布尔转换器
发布时间:2020-12-16 00:09:15 所属栏目:百科 来源:网络整理
导读:我经历过这个 How to bind RadioButtons to an enum? 并接受这个问题的答案包含使用泛型Enum到布尔转换器. 我的问题是我在View中有两个单选按钮和一个枚举 public Enum LinkType { A,B,C,D,E,F } 在ViewModel中,我有一个Called属性 public LinkType MyLinkty
我经历过这个
How to bind RadioButtons to an enum?
并接受这个问题的答案包含使用泛型Enum到布尔转换器. 我的问题是我在View中有两个单选按钮和一个枚举 public Enum LinkType { A,B,C,D,E,F } 在ViewModel中,我有一个Called属性 public LinkType MyLinktype { get;set; } 如果ViewModel中enum的属性具有A,E中的值,则第一个单选按钮可以为true,如果ViewModel中的枚举属性具有值,则第二个单选按钮可以为true. 那么,如何在通用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,System.Globalization.CultureInfo culture) { string parameterString = parameter as string; if (parameterString == null) return DependencyProperty.UnsetValue; return Enum.Parse(targetType,parameterString); } 那么如果我想在XAML中使用这样的东西,我必须在转换器中做出哪些改变 <RadioButton IsChecked="{Binding Path=MyLinktype,Converter={StaticResource enumBooleanConverter},ConverterParameter=A,F}">Odd LinkType</RadioButton> <RadioButton IsChecked="{Binding Path=Mylinktype,ConverterParameter=B,E}">Even Link Type</RadioButton> 解决方法
您可以在xaml中定义一个数组:
<x:Array Type="LinkType" x:Key="ar"> <LinkType>A</LinkType> <LinkType>B</LinkType> </x:Array> 然后将其作为参数传递 <RadioButton IsChecked="{Binding Path=MyLinktype,ConverterParameter={StaticResource ar}}">Odd LinkType</RadioButton> 您必须修复转换器,以便正确处理数组作为转换器参数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |