c# - 如何将通用枚举中的多个转换器参数传递给 bool 转换器

标签 c# wpf xaml converters

我经历过这个How to bind RadioButtons to an enum?

这个问题的公认答案包含通用枚举到 bool 转换器的使用。

我的问题是我在 View 中有两个单选按钮和一个枚举

 public Enum LinkType
   {
       A,
       B,
       C,
       D,
       E,
       F
    }

在 ViewModel 中我有一个属性 Called

public LinkType MyLinktype
{
  get;set;
}

如果 ViewModel 中的枚举属性在 A、C、E 之间具有值,我的第一个单选按钮可以为真,如果 ViewModel 中的枚举属性在其中具有值,则第二个单选按钮可以为真。 B,D,F

那么,如何在通用 EnumTo bool 转换器的转换器参数中传递多个值,如下所示

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

如果我想在 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>

最佳答案

你可以在xaml中定义一个数组:

        <x:Array Type="LinkType" x:Key="ar">
            <LinkType>A</LinkType>
            <LinkType>B</LinkType>
        </x:Array>

然后将其作为参数传递

<RadioButton IsChecked="{Binding Path=MyLinktype, Converter={StaticResource enumBooleanConverter}, ConverterParameter={StaticResource ar}}">Odd LinkType</RadioButton>

您必须修复您的转换器,以便正确处理数组作为转换器参数。

关于c# - 如何将通用枚举中的多个转换器参数传递给 bool 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18864627/

相关文章:

c# - 将小数转换为对象

c# - 如何在asp.net中制作多语言网站

c# - Alt 标签在 IE 中显示不正确

wpf - 创建线框 3D 立方体

c# - 双向绑定(bind)问题 - PropertyChanged 和 CanExecuteChanged

c# - 在 WP7 中手动退出文本框的键入模式

.net - 如何在 ResourceDictionary 中存储 HTML 字符串?

c# - 如何接受<>指定另一个键?或者更快的方法来区分两个巨大的 List<>?

c# - WPF 中的验证错误样式,类似于 Silverlight

c# - 多次显示和隐藏带有幻灯片动画的 WPF 窗口,同时每次都一致地显示动画