我遇到了以下问题。我创建了一个带有几个DependencyProperties的按钮的子类。我们正试图在风格中使用这些。
我们有以下c#
/// <summary>
/// Custom Is Mouse Over Colour.
/// </summary>
public static DependencyProperty LITIsMouseOverProperty =
DependencyProperty.RegisterAttached("LITIsMouseOver",
typeof(System.Windows.Media.LinearGradientBrush), typeof(Button));
public static System.Windows.Media.LinearGradientBrush GetLITIsMouseOver(DependencyObject target)
{
return (System.Windows.Media.LinearGradientBrush)target.GetValue(LITIsMouseOverProperty);
}
public static void SetLITIsMouseOver(DependencyObject target, System.Windows.Media.LinearGradientBrush value)
{
target.SetValue(LITIsMouseOverProperty, value);
}
以及以下XAML:
<Style x:Key="StandardButton" TargetType="{x:Type Utils:LITCustomButton01}">
<Setter Property="LITIsMouseOver" Value="{StaticResource DarkBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Utils:LITCustomButton01}">
<Border x:Name="buttonBorder" CornerRadius="{TemplateBinding LITCornerRadius}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<TextBlock Name="textBlock" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{TemplateBinding LITIsMouseOver}" />
</Trigger>
</Style.Triggers>
</Style>
我们有相同的系统在角半径上工作,使用以下内容:
/// <summary>
/// Custom Corner Radius
/// </summary>
public static DependencyProperty LITCornerRadiusProperty =
DependencyProperty.RegisterAttached("LITCornerRadius",
typeof(CornerRadius), typeof(Button));
public static CornerRadius GetLITCornerRadius(DependencyObject target)
{
return (CornerRadius)target.GetValue(LITCornerRadiusProperty);
}
public static void SetLITCornerRadius(DependencyObject target, CornerRadius value)
{
target.SetValue(LITCornerRadiusProperty, value);
}
但是,后台会抛出以下错误:
“成员无效,因为它没有合格的类型名称。”
答案 0 :(得分:1)
TemplateBinding
只能在ControlTemplate
中使用。 TemplateBinding
快捷方式在触发器中不起作用,因此您必须使用RelativeSource
绑定。覆盖ControlTemplate
时,您需要指定触发器的目标。像这样:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
TargetName="buttonBorder"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LITIsMouseOver}"/>
</Trigger>
</ControlTemplate.Triggers>
此外,您正在创建一个附加属性,允许您执行以下操作:
<Grid Utils:LITCustomButton01.LITIsMouseOver="Pink"/>
在这种情况下,MouseOver
不会发生任何事情。您想使用Register
而不是RegisterAttached
。