我用ControlTemplate
创建了一个自定义按钮。
我使用ControlTemplate
来绑定UserControl
的默认属性,例如Background
或Foreground
。
但是,如果我添加自定义依赖项属性(例如CornerRadius
),则会出现两个错误:
Xaml:
<UserControl x:Class="MyProject.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50"
BorderThickness="1" BorderBrush="White" Background="Black"
Content="OK" Foreground="White">
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<Border Name="ground"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<Label Name="content"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontWeight="{TemplateBinding FontWeight}"
FontSize="{TemplateBinding FontSize}"/>
</Border>
</ControlTemplate>
</UserControl.Template>
</UserControl>
代码背后:
namespace MyProject
{
public partial class MyButton : UserControl
{
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MyButton));
public CornerRadius CornerRadius
{
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
public MyButton() => InitializeComponent();
}
}
如果我使用此处User control with custom properties指定的解决方案,则会遇到此问题Wpf - Custom control: How to override default properties?。
那么,有一种解决方案可以避免这两个问题?
答案 0 :(得分:1)
自定义Button不应是UserControl,而应直接从Button派生。
向您的Visual Studio项目添加“自定义控件(WPF)”并按如下所示对其进行修改:
public class MyButton : Button
{
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(MyButton),
new FrameworkPropertyMetadata(typeof(MyButton)));
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register(
nameof(CornerRadius), typeof(CornerRadius), typeof(MyButton));
public CornerRadius CornerRadius
{
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
}
然后在生成的Themes\Generic.xaml
文件中更改其默认样式:
<Style TargetType="local:MyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<Border Name="ground"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<Label Name="content"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
FontFamily="{TemplateBinding FontFamily}"
FontWeight="{TemplateBinding FontWeight}"
FontSize="{TemplateBinding FontSize}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有关详细信息,请参见Control Authoring Overview。