【问题标题】:How to use specific properties of a Value converter in wpf xaml如何在 wpf xaml 中使用值转换器的特定属性
【发布时间】:2016-08-16 08:59:13
【问题描述】:

我有一个枚举到字符串转换器

public class EnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        MailSettingsStateEnum enumValue = (MailSettingsStateEnum)value;

        // extension method on the enum, to return a string based on enum.
        return enumValue.Description(); 
    }

    // ConvertBack not relevant here.
}

我很容易在 wpf xaml 中使用它来设置标签的 Content 属性。

<Label Content="{Binding MailSettingState, Converter={StaticResource 
EnumConverterString}}" 
BorderBrush="{Binding MailSettingState, Converter={StaticResource 
EnumConverterBorderBrush}}" />

现在你可以看到,我有另一个属性BorderBrush。我还必须基于相同的枚举进行设置。所以我不得不写另一个转换器EnumConverterBorderBrush

那么有没有一种方法可以让我只有一个转换器,它返回一个具有两个属性的对象,我可以在 xaml 中使用这些属性?我可以创建转换器,很简单,但我不知道如何在 xaml 中使用它。假设转换器返回了一个对象,并且有两个名为 MessageString(字符串类型)的属性,以及另一个 Brush 类型的 BorderBrush,我该如何在 xaml 中使用它?

【问题讨论】:

  • AFAIK 不可能满足您的要求。转换器将返回您想要的任何对象,但输出仍然是一个结果。
  • 对我来说听起来有点 hacky,但您可以尝试将 LabelDataContext 设置为转换后的对象(您需要为此创建一个类),然后绑定到它们的属性。不过,我只会使用两个单独的转换器。编辑。经过测试和工作。
  • 谢谢大家。我才意识到自己的愚蠢。我想要的方式,这是不可能的。所有三个答案都是解决方案,但由于我只能选择一个,因此为其他两个道歉。

标签: wpf ivalueconverter


【解决方案1】:

您可以根据转换器中收到的targetType 切换输出。

所以你可以这样做:

public class EnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        var enumValue = (MailSettingsStateEnum)value;

        switch(targetType)
        {
            case typeof(string)
                return enumValue.Description(); 
            case typeof(Brush)
                return enumValue.GetBrush();
            default:
                throw new NotSupportedException("Type not supported")
        }
    }

    // ConvertBack not relevant here.
}

现在您将拥有一个转换器来统治他们!

【讨论】:

    【解决方案2】:

    转换器应返回与请求的targetType 匹配的对象。转换器可以根据parameter 为输入enum 值返回不同的值。我认为它比仅依赖targetType 更灵活。

    public class SpecEnumConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Enum)
            {                
                if ((string) parameter == "brush")
                    return "Red"; // return brush here!
    
                // if not pre-defined parameter (null or any other), return description
                return (int) value; // return enum description here!
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    用法:

    <Label Content="{Binding MailSettingState, Converter={StaticResource 
    EnumConverterSpec}}" 
    BorderBrush="{Binding MailSettingState, Converter={StaticResource 
    EnumConverterSpec}, ConverterParameter='brush'}" />
    

    【讨论】:

      【解决方案3】:

      我已经在上面发表了评论,但这是解决方案。

      <Label DataContext="{Binding MailSettingState, Converter={converters:EnumConverter}}" Content="{Binding Label}" BorderBrush="{Binding BorderBrush}"/>
      
      public class EnumConverter: MarkupExtension, IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
              var enumValue = (MailSettingsStateEnum) value;
              return new ConvertedEnum { Label = enumValue.Description(), 
                                         BorderBrush = new BorderBrush()};
      
          }
      
          // ConvertBack not relevant here.
      
          public override object ProvideValue(IServiceProvider serviceProvider)
          {
              return this;
          }
      } 
      
      public class ConvertedEnum
      {
          public string Label {get; set;}
          public BorderBrush {get; set;}
      }
      

      在我看来,单独的转换器仍然更漂亮。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-01
        • 2011-02-25
        • 2012-11-21
        • 2018-03-12
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        相关资源
        最近更新 更多