无法使用 XAML 中的触发器更改背景颜色
我有一个小问题,我不知道如何解决它......
我从按钮创建了一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用:
Try using instead: