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

I have a UserControl that has a button on it. UserControl has a DependencyProperty called IsNew. This is a bool value set to true if the object being edited in the control has been newly created and has not yet been written to the database. Otherwise, it is wrong.

I have a button that currently reads "Save". I was asked to change the button so that it reads "Insert" if the line is new. Now I have the XAML for the button:

<Button Background="{DynamicResource ButtonBackground}" 
        Foreground="{DynamicResource ButtonForeground}" 
        IsEnabled="{Binding Path=IsDirty, RelativeSource={RelativeSource AncestorType={x:Type cs:Editor}}}"
        Visibility="{Binding Path=CanModify, Converter={StaticResource BoolToVisibility}}">
        <Trigger Property="Editor.IsNew" Value="True">
            <Setter Property="Button.Content" Value="Insert" />
        <Trigger Property="Editor.IsNew>
            <Setter Property="Button.Content" Value="Save" />


I am getting a runtime exception, 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 code, but I want to do it in XAML.




source to share

2 answers

You can only use EventTriggers in Control.Triggers. To use a different type of trigger (Trigger, DataTrigger), you must use the style:

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


And your Property="Editor.IsNew"

and Property="Button.Content"

will not work because it triggers belong to a Button, and try to find the trigger property "Editor.IsNew" button. You can fix this by changing the trigger to DataTrigger and linking the RelativeSource with your UserControl and its IsNew property. And Property="Button.Content

"must be changed to Property="Content"




The reason for this is that the control can implement EventTriggers. The other types of triggers you need need to be styled and then your button will use that styling.



All Articles