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

发布于 2024-08-19 22:46:04 字数 1439 浏览 1 评论 0原文

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

我从按钮创建了一个名为“StandardKeyView”的自定义控件。该控件有一个依赖属性“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这个类型不错,那为什么用在触发器中就不行呢?

I have a little problem and I don't know how can I fix it ...

I created a CustomControl called "StandardKeyView" from a Button. This control has a dependency property "DownImage" which is used to define a Background Image to my control during the mouse over.

Here's the definition of the DownImage property :

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

Here's the definition of a new StandardKeyView :

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

Here's the default control template of a 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>

I think it should work fine, but not really. I have a weird error : when I try to start the application, I have a XamlParseException who said

Unable to convert the value of attribute 'Value' as an object of type ''.

I think the type of DownImage is good, then why doesn't it work when it used in the trigger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-08-26 22:46:04

尝试使用:

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

Try using instead:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DownImage}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文