尝试向按钮添加触发器以更改按钮的Content属性 [英] Trying to add a trigger to a button to change the button's Content property

查看:171
本文介绍了尝试向按钮添加触发器以更改按钮的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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆