无法使用XAML中的触发器更改背景颜色

时间:2010-01-31 15:17:12

标签: wpf templates triggers wpf-controls

我有一点问题,我不知道如何解决它...

我从Button创建了一个名为“StandardKeyView”的CustomControl。此控件具有依赖项属性“DownImage”,用于在鼠标悬停期间为我的控件定义背景图像。

这是DownImage属性的定义:

public SolidColorBrush DownImage
    {
        get { return (SolidColorBrush)GetValue( DownImageProperty ); }
        set { SetValue( DownImageProperty, value ); }
    }
    public static readonly DependencyProperty DownImageProperty = 
DependencyProperty.Register( "DownImage", typeof( SolidColorBrush ), typeof( StandardKeyView ) );

以下是新StandardKeyView的定义:

<skv:StandardKeyView Background="White" DownImage="Black"/>

这是StandardKeyView的默认控件模板:

    <ControlTemplate TargetType="{x:Type skv:StandardKeyView}" x:Key="DefaultStandardKeyViewTemplate">
    <Button Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" x:Name="MainButton" />
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="MainButton" Property="Background" Value="{TemplateBinding Property=DownImage}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

我认为它应该可以正常工作,但不是真的。我有一个奇怪的错误:当我尝试启动应用程序时,我有一个XamlParseException谁说

  

无法将属性'Value'的值转换为''。

类型的对象

我认为DownImage的类型很好,那么为什么它在触发器中使用时不起作用呢?

1 个答案:

答案 0 :(得分:1)

请尝试使用:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DownImage}"