wpf 自定义控件发送刷像 TemplateBinding
wpf custom control send brush like TemplateBinding
您好,我为按钮创建了自定义控件,但是发送画笔时出现问题
如果您在下面看到我在 Radius 及其作品之前创建的,但 Brush 不是。
注意:如果我放一张图片来显示错误会更好,而不是直接放代码
这里是来源:
此处为新代码:
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>
不要在 Setter
中使用 {TemplateBinding}
语法。这应该有效:
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Background"
Value="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=MouseHoverColor}">
</Setter>
</Trigger>
您好,我为按钮创建了自定义控件,但是发送画笔时出现问题 如果您在下面看到我在 Radius 及其作品之前创建的,但 Brush 不是。 注意:如果我放一张图片来显示错误会更好,而不是直接放代码
这里是来源:
此处为新代码:
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>
不要在 Setter
中使用 {TemplateBinding}
语法。这应该有效:
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Background"
Value="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=MouseHoverColor}">
</Setter>
</Trigger>