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

thank

Tony

+3


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:

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

      



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"

.

+9


source


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.



0


source







All Articles