尝试向按钮添加触发器以更改按钮的Content属性

编程入门 行业动态 更新时间:2024-10-26 12:27:13
本文介绍了尝试向按钮添加触发器以更改按钮的Content属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有按钮的UserControl。 UserControl具有一个名为IsNew的DependencyProperty。这是一个布尔值,如果在控件中正在编辑的对象是新创建的并且尚未写入数据库,则将其设置为true。否则它是错误的。

I've got a UserControl that has a button on it. The UserControl has a DependencyProperty called IsNew. This is a bool value that is set to true if the object being edited in the control was newly created and hasn't been written to the database yet. It is false otherwise.

我有一个当前显示为保存的按钮。我被要求更改按钮,如果该行是新行,它将显示为插入。我现在在按钮下面有XAML:

I've got a button that currently reads "Save". I've been asked to change the button so it reads "Insert" if the row is new. I now have the XAML below for the button:

<Button Background="{DynamicResource ButtonBackground}" Click="SaveButton_Click" Content="Save" FontSize="20" FontWeight="Bold" Foreground="{DynamicResource ButtonForeground}" Height="60" IsEnabled="{Binding Path=IsDirty, RelativeSource={RelativeSource AncestorType={x:Type cs:Editor}}}" Margin="5" Name="SaveButton" TabIndex="7" Visibility="{Binding Path=CanModify, Converter={StaticResource BoolToVisibility}}"> <Button.Triggers> <Trigger Property="Editor.IsNew" Value="True"> <Setter Property="Button.Content" Value="Insert" /> </Trigger> <Trigger Property="Editor.IsNew> <Setter Property="Button.Content" Value="Save" /> </Trigger> </Button.Triggers> </Button>

我遇到了一个例外在运行时,其内部异常显示为:

I'm getting an exception at run time, whose inner exception reads:

Triggers collection members must be of type EventTrigger.

如何使它起作用?我可以在后面的代码中轻松地做到这一点,但我想在XAML中做到这一点。

How do I get this to work? I could do this easily in the code-behind, but I want to do it in the XAML.

谢谢

Tony

推荐答案

您只能在Control.Triggers中使用EventTriggers。要使用其他类型的触发器(Trigger,DataTrigger),则应使用样式:

You can use only EventTriggers in Control.Triggers. To use other kind of trigger (Trigger, DataTrigger) you should use style:

<Button.Style> <Style TargetType="Button"> <Style.Triggers> <Trigger Property="Editor.IsNew" Value="True"> <Setter Property="Button.Content" Value="Insert" /> </Trigger>

还有您的 Property = Editor.IsNew 和 Property = Button.Content 将不起作用,因为触发器属于Button,触发器将尝试在button上查找属性 Editor.IsNew。您可以通过将触发器更改为DataTrigger并将其与RelativeSource绑定到UserControl及其IsNew属性来进行修复。并且 Property = Button.Content 需要更改为 Property = Content 。

And also your Property="Editor.IsNew" and Property="Button.Content" won't work because triggers belongs to Button and trigger will try to find property "Editor.IsNew" on button. You can fix it by changing trigger to DataTrigger and bind with RelativeSource to your UserControl and its IsNew property. And Property="Button.Content" needs to be changed to Property="Content".

更多推荐

尝试向按钮添加触发器以更改按钮的Content属性

本文发布于:2023-11-12 05:02:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   触发器   属性   Content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!