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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9789961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:19:50  来源:igfitidea点击:

Trying to add a trigger to a button to change the button's Content property

wpftriggers

提问by Tony Vitabile

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.

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

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:

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

<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.

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.

我如何让这个工作?我可以在代码隐藏中轻松做到这一点,但我想在 XAML 中做到这一点。

Thanks

谢谢

Tony

托尼

回答by Nikolay

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

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

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

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".

而且您的Property="Editor.IsNew"Property="Button.Content"将不起作用,因为触发器属于 Button 并且触发器将尝试在按钮上查找属性“Editor.IsNew”。您可以通过将触发器更改为 DataTrigger 并使用 RelativeSource 绑定到您的 UserControl 及其 IsNew 属性来修复它。并且Property="Button.Content" 需要更改为Property="Content".

回答by NestorArturo

The reason for this is that a control can only implement EventTriggers. The other kind of triggers you need have to be implemented in a style, and then your button uses that style.

这样做的原因是控件只能实现 EventTriggers。您需要的另一种触发器必须以某种样式实现,然后您的按钮使用该样式。