WPF:如何注册额外的内含价值转换器? [英] WPF: How do I register additional implicit value converters?

查看:113
本文介绍了WPF:如何注册额外的内含价值转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个问题,询问如何避免添加自定义值转换器,以一个人的应用程序资源:

I found a question asking about ways to avoid adding custom value converters to one's application resources:

在WPF应用价值转换器,而无需将其定义为资源第一

不过,我想多走了一步除此之外并注册转换器,然后隐式的,如下面的例子:

However I'd like to go one step beyond that and register converters that are then implicit, as in this example:

<SolidColorBrush Color="Blue" />

在这里,我假设了一些隐含的StringToSolidColorBrushConverter被踢入,使这个例子的工作。

Here, I am assuming that some implicit "StringToSolidColorBrushConverter" is kicking-in that makes the example work.

这个例子做的不可以工作

<Window.Resources>
    <Color x:Key="ForegroundFontColor">Blue</Color>
</Window.Resources>

<TextBlock Foreground={StaticResource ForegroundFontColor}>Hello</TextBlock>

我相信这是因为没有implcit Col​​orToSolidColorBrushConverter 的WPF只需拿起和使用。我知道如何创建一个,但我怎么会注册它使WPF使用它自动地没有在绑定前pression指定转换呢?

I believe that's because there is no implcit ColorToSolidColorBrushConverter that WPF can just pick up and use. I know how to create one, but how would I "register" it so that WPF uses it automagically without specifying the converter in the binding expression at all?

推荐答案

如果你看一下源$ C ​​$ C,你会发现这个

If you look at the source code you'll find this

public sealed class SolidColorBrush : Brush
{
  public Color Color
  { ... }
  ...
}

[TypeConverter(typeof (ColorConverter))]
public struct Color : IFormattable, IEquatable<Color>
{
    ...
}

的转换由ColorConverter进行。

The conversion is done by the ColorConverter.

和也

[TypeConverter(typeof (BrushConverter))]
public abstract class Brush : Animatable, IFormattable, DUCE.IResource
{ ... }

public class TextBlock : ...
{  
   public Brush Foreground
   { ... }
}

当转换由BrushConverter完成。

Where the conversion is done by BrushConverter.

有没有'隐'的转换,你可以注册。这一切都完成的应用类型转换器与适当的值转换器相关的属性或类的类型属性。

There's no 'implicit' conversion that you can register. It's all done by applying TypeConverter attributes with the type of the appropriate value converter to relevant properties or classes.

在你的榜样,你需要使用

In your example you need to use

<Window.Resources>
    <SolidColorBrush x:Key="ForegroundFontColor" Color="Blue"/>
</Window.Resources>

<TextBlock Foreground={StaticResource ForegroundFontColor}>Hello</TextBlock>

这篇关于WPF:如何注册额外的内含价值转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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