wpf自定义控件像TemplateBinding一样发送画笔
您好,我为按钮创建了一个自定义控件,但发送画笔时出现问题 如果你在下面看到我在半径及其工作之前创建的,但画笔不是。 注意:如果我放一张图片来显示错误,而不是直接放代码
此处来源:
此处新代码:
public class WPFButton : ButtonBase
{
public CornerRadius Radius
{
get { return (CornerRadius)GetValue(RadiusProperty); }
set { SetValue(RadiusProperty, value); }
}
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(CornerRadius), typeof(WPFButton), new PropertyMetadata(new CornerRadius(0)));
public Brush MouseHoverColor
{
get { return (Brush)GetValue(MouseHoverColorProperty); }
set { SetValue(MouseHoverColorProperty, value); }
}
public static readonly DependencyProperty MouseHoverColorProperty =
DependencyProperty.Register("MouseHoverColor", typeof(Brush), typeof(WPFButton), new PropertyMetadata(new SolidColorBrush()) );
static WPFButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFButton), new FrameworkPropertyMetadata(typeof(WPFButton)));
}
}
Xaml
<Style TargetType="{x:Type local:WPFButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:WPFButton}">
<Border Name="Background" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Radius}">
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Background" Value="{TemplateBinding MouseHoverColor}">
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="Background" Value="#FF4792DC">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
hi i created a custom control for button but there is problem in send Brush
if you see down below i created before Radius and its work but the Brush is not.
Note: It would be better if I put a picture to show the errors, rather than putting the code directly
and here the source :
here new code :
public class WPFButton : ButtonBase
{
public CornerRadius Radius
{
get { return (CornerRadius)GetValue(RadiusProperty); }
set { SetValue(RadiusProperty, value); }
}
public static readonly DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(CornerRadius), typeof(WPFButton), new PropertyMetadata(new CornerRadius(0)));
public Brush MouseHoverColor
{
get { return (Brush)GetValue(MouseHoverColorProperty); }
set { SetValue(MouseHoverColorProperty, value); }
}
public static readonly DependencyProperty MouseHoverColorProperty =
DependencyProperty.Register("MouseHoverColor", typeof(Brush), typeof(WPFButton), new PropertyMetadata(new SolidColorBrush()) );
static WPFButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WPFButton), new FrameworkPropertyMetadata(typeof(WPFButton)));
}
}
Xaml
<Style TargetType="{x:Type local:WPFButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:WPFButton}">
<Border Name="Background" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding Radius}">
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Background" Value="{TemplateBinding MouseHoverColor}">
</Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="Background" Value="#FF4792DC">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在
Setter
中使用{TemplateBinding}
语法。这应该有效:Do not use the
{TemplateBinding}
syntax in aSetter
. This should work: