提高的IValueConverter - 的MarkupExtension或DependencyObject的? [英] Improved IValueConverter -- MarkupExtension or DependencyObject?

查看:228
本文介绍了提高的IValueConverter - 的MarkupExtension或DependencyObject的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上看到了两种不同的方法来增强一个的IValueConverter。其中一人来自的MarkupExtension自DependencyObject延伸出ValueConverter,其他。我无法从两个延伸,所以我不知道如果有一个比其他更好的?

I saw online 2 different approaches to enhancing an IValueConverter. One of them extended a ValueConverter from MarkupExtension, the other from DependencyObject. I can't extend from both, so I'm wondering if any one is better than the other?

推荐答案

从每个派生给你别样的功能和灵活性:

Deriving from each gives you different kind of power and flexibility:


  • 的MarkupExtension 派生使您可以使用值转换器,而不使其成为一个静态资源,如下所述:

  • Deriving from MarkupExtension enables you to use the value converter without making it a static resource, as described below:

public class DoubleMe : MarkupExtension, IValueConverter
{
   public override object ProvideValue(IServiceProvider serviceProvider)
   {
      return this;
   }
   public object Convert(object value, /*rest of parameters*/ )
   {
      if ( value is int )
         return (int)(value) * 2; //double it
      else
         return value.ToString() + value.ToString();
   }
  //...
}

在XAML ,你可以直接使用它,而无需创建静态资源:

In XAML, you can directly use it without creating a StaticResource:

<TextBlock Text="{Binding Name, Converter={local:DoubleMe}}"/>
<TextBlock Text="{Binding Age, Converter={local:DoubleMe}}"/>



这样的代码是非常方便的调试时,你可以只写地方: DebugMe 然后可以调试在其上使用该控件的DataContext的。

Such code is very handy when debugging, as you can just write local:DebugMe and then can debug the DataContext of the control on which you use it.

从派生的DependencyObject 可让您的配置的一些喜好值转换器的更表达方式的,如下所述:

Deriving from DependencyObject enables you to configure the value converter with some preferences in a more expressive way, as described below:

public class TruncateMe : DependencyObject, IValueConverter
{
     public static readonly DependencyProperty MaxLengthProperty =
         DependencyProperty.Register("MaxLength",
                                      typeof(int),
                                      typeof(TruncateMe),
                                      new PropertyMetadata(100));
     public int MaxLength
     {
         get { return (int) this.GetValue(MaxLengthProperty); }
         set { this.SetValue(MaxLengthProperty, value); }
     }

     public object Convert(object value, /*rest of parameters*/ )
     {
        string s = value.ToString();
        if ( s.Length > MaxLength)
          return s.Substring(0, MaxLength) + "...";
      else
          return s;
     }
     //...
}

在XAML ,你可以直接使用它作为:

In XAML, you can directly use it as:

<TextBlock>
   <TextBlock.Text>
       <Binding Path="FullDescription">
           <Binding.Converter>
             <local:TruncateMe MaxLength="50"/>
           </Binding.Converter>
       </Binding>
   </TextBlock.Text> 



这是什么呢?它截断字符串<​​code> FullDescription ,如果它比 50 字符的更多!

What does it do? It truncates the string FullDescription if it is more than 50 characters!

@crazyarabian评论说:

@crazyarabian commented that:

您声明自DependencyObject派生使您可以配置以更表达方式的一些喜好值转换器是不是独家的DependencyObject,你可以导致℃的的MarkupExtension创建相同的MaxLength属性,TextBlock的文本=结合年龄,转换器= {地方:DoubleMe,最大长度= 50}}/> 。我认为一个的MarkupExtension更表现力和更简洁。

Your statement "Deriving from DependencyObject enables you to configure the value converter with some preferences in a more expressive way" isn't exclusive to DependencyObject as you can create the same MaxLength property on a MarkupExtension resulting in <TextBlock Text="Binding Age, Converter={local:DoubleMe, MaxLength=50}}"/>. I would argue that a MarkupExtension is more expressive and less verbose.

这是真实的。但后来不是绑定;也就是说,当您从的MarkupExtension 派生,那么你可以这样做:

That is true. But then that is not bindable; that is, when you derive from MarkupExtension, then you cannot do :

MaxLength="{Binding TextLength}"

但是,如果从派生您转炉 DependencyObject的,那么你可以做以上。在这个意义上,它是的更传神的相比的MarkupExtension

But if you derive your converter from DependencyObject, then you can do the above. In that sense, it is more expressive compared to MarkupExtension.

注意目标属性必须是一个的DependencyProperty 绑定工作。 MSDN说,

Note that the target property must be a DependencyProperty for Binding to work. MSDN says,


  • 通常,每个绑定有四个组成部分:一个约束性目标
    对象,目标属性,绑定
    源,并在
    结合源的值的路径。例如,如果
    要在
    的TextBox到
    Employee对象的Name属性的内容绑定,你的目标对象是
    文本框,目标属性是
    Text属性
    ,使用的值是
    名称,并且源对象是
    Employee对象。

  • Each binding typically has these four components: a binding target object, a target property, a binding source, and a Path to the value in the binding source to use. For example, if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.

目标属性必须是依赖属性。

这篇关于提高的IValueConverter - 的MarkupExtension或DependencyObject的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆