Background="Red"中的字符串"Red"如何转换为SolidColorBrush?
How is the string "Red" in Background="Red" converted to SolidColorBrush?
我想了解WPF是如何将字符串值(Red
)转换成下面案例中对应的SolidColorBrush
的?
我们如何对我们的习惯做同样的事情 DependencyProperty
?
<Button Background="Red" />
从字符串到 Brush 的转换是由一个 BrushConverter 实例执行的,一个 TypeConverter 是这样注册的:
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.BrushConverter))]
public abstract class Brush : ...
它将自动用于任何 属性 刷子类型。
假设您有 属性 的依赖关系
public static readonly DependencyProperty MyBrushProperty =
DependencyProperty.Register(
nameof(MyBrush),
typeof(Brush),
typeof(MyButton));
public Brush MyBrush
{
get { return (Brush)GetValue(MyBrushProperty); }
set { SetValue(MyBrushProperty, value); }
}
以下开箱即用:
<local:MyButton MyBrush="Red" />
如果问题是关于自定义 属性 类型 - 而不是刷类型的自定义 属性 - 请参阅其他答案:-)
这是使用类型转换器完成的,请参阅 TypeConverters and XAML。
This topic introduces the purpose of type conversion from string as a general XAML language feature. In the .NET Framework, the TypeConverter class serves a particular purpose as part of the implementation for a managed custom class that can be used as a property value in XAML attribute usage. If you write a custom class, and you want instances of your class to be usable as XAML settable attribute values, you might need to apply a TypeConverterAttribute to your class, write a custom TypeConverter class, or both.
A XAML processor needs two pieces of information in order to process an attribute value. The first piece of information is the value type of the property that is being set. Any string that defines an attribute value and that is processed in XAML must ultimately be converted or resolved to a value of that type. [...]
If the value is neither a parser-understood primitive nor an enumeration, then the type in question must be able to provide an instance of the type, or a value, based on a converted string. This is done by indicating a type converter class. The type converter is effectively a helper class for providing values of another class, both for the XAML scenario and also potentially for code calls in .NET code.
WPF 有一些内置类型转换器,例如 Brush
, the BrushConverter
. You can see how the brush converter is implemented in .NET e.g. in the reference source or on GitHub for .NET Core。
从documentation of Brush
可以看出,它为转换器指定了一个属性。
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.BrushConverter))]
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public abstract class Brush : System.Windows.Media.Animation.Animatable, IFormattable
您不是为依赖项 属性 创建类型转换器,而是为它的基础类型创建类型转换器,例如 Brush
。 documenation 详细展示了如何为自定义类型创建类型转换器。本质上:
创建源自 TypeConverter
的自定义类型转换器。
public sealed class MyCustomTypeConverter : TypeConverter
{
// ...conversion methods.
}
实现以下方法。
最后,将 TypeConverterAttribute
应用于您的自定义类型。
In order for your custom type converter to be used as the acting type converter for a custom class by a XAML processor, you must apply the TypeConverterAttribute to your class definition. The ConverterTypeName that you specify through the attribute must be the type name of your custom type converter. With this attribute applied, when a XAML processor handles values where the property type uses your custom class type, it can input strings and return object instances.
[TypeConverter(typeof(MyCustomTypeConverter ))]
public class YourCustomType
{
// ...code of your custom type.
}
按照这些步骤,XAML 解析器将能够自动将字符串转换为您的类型。
上面提到的 BrushConverter 是 TypeConverter
实施的一个很好的例子。
我想了解WPF是如何将字符串值(Red
)转换成下面案例中对应的SolidColorBrush
的?
我们如何对我们的习惯做同样的事情 DependencyProperty
?
<Button Background="Red" />
从字符串到 Brush 的转换是由一个 BrushConverter 实例执行的,一个 TypeConverter 是这样注册的:
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.BrushConverter))]
public abstract class Brush : ...
它将自动用于任何 属性 刷子类型。
假设您有 属性 的依赖关系
public static readonly DependencyProperty MyBrushProperty =
DependencyProperty.Register(
nameof(MyBrush),
typeof(Brush),
typeof(MyButton));
public Brush MyBrush
{
get { return (Brush)GetValue(MyBrushProperty); }
set { SetValue(MyBrushProperty, value); }
}
以下开箱即用:
<local:MyButton MyBrush="Red" />
如果问题是关于自定义 属性 类型 - 而不是刷类型的自定义 属性 - 请参阅其他答案:-)
这是使用类型转换器完成的,请参阅 TypeConverters and XAML。
This topic introduces the purpose of type conversion from string as a general XAML language feature. In the .NET Framework, the TypeConverter class serves a particular purpose as part of the implementation for a managed custom class that can be used as a property value in XAML attribute usage. If you write a custom class, and you want instances of your class to be usable as XAML settable attribute values, you might need to apply a TypeConverterAttribute to your class, write a custom TypeConverter class, or both.
A XAML processor needs two pieces of information in order to process an attribute value. The first piece of information is the value type of the property that is being set. Any string that defines an attribute value and that is processed in XAML must ultimately be converted or resolved to a value of that type. [...]
If the value is neither a parser-understood primitive nor an enumeration, then the type in question must be able to provide an instance of the type, or a value, based on a converted string. This is done by indicating a type converter class. The type converter is effectively a helper class for providing values of another class, both for the XAML scenario and also potentially for code calls in .NET code.
WPF 有一些内置类型转换器,例如 Brush
, the BrushConverter
. You can see how the brush converter is implemented in .NET e.g. in the reference source or on GitHub for .NET Core。
从documentation of Brush
可以看出,它为转换器指定了一个属性。
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.BrushConverter))]
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public abstract class Brush : System.Windows.Media.Animation.Animatable, IFormattable
您不是为依赖项 属性 创建类型转换器,而是为它的基础类型创建类型转换器,例如 Brush
。 documenation 详细展示了如何为自定义类型创建类型转换器。本质上:
创建源自
TypeConverter
的自定义类型转换器。public sealed class MyCustomTypeConverter : TypeConverter { // ...conversion methods. }
实现以下方法。
最后,将
TypeConverterAttribute
应用于您的自定义类型。In order for your custom type converter to be used as the acting type converter for a custom class by a XAML processor, you must apply the TypeConverterAttribute to your class definition. The ConverterTypeName that you specify through the attribute must be the type name of your custom type converter. With this attribute applied, when a XAML processor handles values where the property type uses your custom class type, it can input strings and return object instances.
[TypeConverter(typeof(MyCustomTypeConverter ))] public class YourCustomType { // ...code of your custom type. }
按照这些步骤,XAML 解析器将能够自动将字符串转换为您的类型。
上面提到的 BrushConverter 是 TypeConverter
实施的一个很好的例子。