C# DependencyProperty - 对复杂类型使用默认 属性
C# DependencyProperty - Use default property for complex type
我正在创建一个模仿 AppBarButton
的自定义控件(但具有自定义功能,因此我们无法从 AppBarButton
派生)。
我的问题是 AppBarButton
的 Icon
属性。 属性 本身采用 IconElement
,但如果您正在创建 AppBarButton
并指定 Icon
内联,它将默认为 Symbol
枚举并创建 SymbolIcon
给你。
我的问题是:我将如何复制它?我似乎找不到有关如何执行此操作的任何信息。
谢谢!
您正在寻找的是 XAML 类型转换器。当 属性 的内容可以有 shorthand 符号时,它们允许您创建自定义解析方法。例如,当您键入一个点值 Point="10,25"
时,会有一个内置的解析器从您的字符串中提取 x 和 y 值。
您可以创建自己的。 Tim Heuer has an example here.
IconElement 是这些 class 的父级 class:
所以给AppBarButton
的Icon
分配一个SymbolIcon
是没有问题的属性。在 UWP XAML 系统中,内置了对 SymbolIcon
的类型转换器支持。对于复制,你应该能够定义一个 DependencyProperty 类型是 IconElement
然后像在 AppBarButton
.
中那样使用它
举个简单的例子,我创建了一个名为 "CustomAppBarButton" 的模板化控件,它有一个名为 "Icon".
的依赖项 属性
CustomAppBarButton.cs:
public sealed class CustomAppBarButton : Control
{
public CustomAppBarButton()
{
this.DefaultStyleKey = typeof(CustomAppBarButton);
}
public IconElement Icon
{
get { return (IconElement)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
// Using a DependencyProperty as the backing store for Icon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(IconElement), typeof(CustomAppBarButton), new PropertyMetadata(null));
}
Generic.xaml:
<Style TargetType="local:CustomAppBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomAppBarButton">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter x:Name="Content"
HorizontalAlignment="Stretch"
Content="{TemplateBinding Icon}"
Foreground="{TemplateBinding Foreground}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后就可以像下面这样使用了
<local:CustomAppBarButton Icon="Like" Foreground="Red" />
我正在创建一个模仿 AppBarButton
的自定义控件(但具有自定义功能,因此我们无法从 AppBarButton
派生)。
我的问题是 AppBarButton
的 Icon
属性。 属性 本身采用 IconElement
,但如果您正在创建 AppBarButton
并指定 Icon
内联,它将默认为 Symbol
枚举并创建 SymbolIcon
给你。
我的问题是:我将如何复制它?我似乎找不到有关如何执行此操作的任何信息。
谢谢!
您正在寻找的是 XAML 类型转换器。当 属性 的内容可以有 shorthand 符号时,它们允许您创建自定义解析方法。例如,当您键入一个点值 Point="10,25"
时,会有一个内置的解析器从您的字符串中提取 x 和 y 值。
您可以创建自己的。 Tim Heuer has an example here.
IconElement 是这些 class 的父级 class:
所以给AppBarButton
的Icon
分配一个SymbolIcon
是没有问题的属性。在 UWP XAML 系统中,内置了对 SymbolIcon
的类型转换器支持。对于复制,你应该能够定义一个 DependencyProperty 类型是 IconElement
然后像在 AppBarButton
.
举个简单的例子,我创建了一个名为 "CustomAppBarButton" 的模板化控件,它有一个名为 "Icon".
的依赖项 属性CustomAppBarButton.cs:
public sealed class CustomAppBarButton : Control
{
public CustomAppBarButton()
{
this.DefaultStyleKey = typeof(CustomAppBarButton);
}
public IconElement Icon
{
get { return (IconElement)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
// Using a DependencyProperty as the backing store for Icon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(IconElement), typeof(CustomAppBarButton), new PropertyMetadata(null));
}
Generic.xaml:
<Style TargetType="local:CustomAppBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomAppBarButton">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter x:Name="Content"
HorizontalAlignment="Stretch"
Content="{TemplateBinding Icon}"
Foreground="{TemplateBinding Foreground}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后就可以像下面这样使用了
<local:CustomAppBarButton Icon="Like" Foreground="Red" />