Triggers collection members must be of type EventTrigger Triggers collection members must be of type EventTrigger wpf wpf

Triggers collection members must be of type EventTrigger


From MSDN Docs, as per the (slightly paraphrased) answer from Richard C. McGuire:

DataTriggers can be used with XML tags Style, ControlTemplate and DataTemplate

For example, if you attempt to add a trigger to a TextBlock, it will generate this error:

Error: Triggers collection members must be of type EventTrigger

Why? A Trigger can only be placed inside a Style, ControlTemplate or DataTemplate, and we are trying to place it directly inside a TextBlock.

In this case, the fix is easy: just wrap the trigger in a style, then place this style inside the TextBlock, and the error will disappear.

Here is the error-generating XAML before the fix:

<TextBlock x:Name="Hello" Text="{Binding Hello, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">  <TextBlock.Triggers>      <DataTrigger Binding="{Binding Hello}" Value="GoGreen">          <Setter Property="Foreground" Value="Green" />      </DataTrigger>  </TextBlock.Triggers></TextBlock>

Here is the XAML after the fix:

<TextBlock x:Name="Hello" Text="{Binding Hello, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">    <TextBlock.Style>        <Style TargetType="TextBlock">            <Setter Property="Foreground" Value="Red" />            <Style.Triggers>                <DataTrigger Binding="{Binding Hello}" Value="GoGreen">                    <Setter Property="Foreground" Value="Green" />                </DataTrigger>            </Style.Triggers>        </Style>    </TextBlock.Style></TextBlock>

Here is a sample screenshot showing that if we enter GoGreen, the text goes green:

enter image description here

... and if we enter something else, the text defaults to red:

enter image description here

There is loads of free material on the web about WPF triggers, and all of them do a reasonably good job of explaining the concept, and this page was the one that made the penny drop for me.


Managed to figure it out. Forgot that DataTriggers are meant for Style, ControlTemplate and DataTemplate per the MSDN Docs.

Solution was to use an EventTrigger as the error message indicated. My solution was the following:

<EventTrigger RoutedEvent="ToggleButton.Checked">    <BeginStoryboard>        <Storyboard>            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"                                           Storyboard.TargetName="LaunchButtons">                <DiscreteObjectKeyFrame KeyTime="0:0:0"                                        Value="{x:Static Visibility.Visible}" />            </ObjectAnimationUsingKeyFrames>        </Storyboard>    </BeginStoryboard></EventTrigger><EventTrigger RoutedEvent="ToggleButton.Unchecked">    <BeginStoryboard>        <Storyboard>            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"                                           Storyboard.TargetName="LaunchButtons">                <DiscreteObjectKeyFrame KeyTime="0:0:0"                                        Value="{x:Static Visibility.Collapsed}" />            </ObjectAnimationUsingKeyFrames>        </Storyboard>    </BeginStoryboard> </EventTrigger>

Going to hold off marking this as the answer in case someone else has another solution.


You could also bind the Visibility in your stackpanel to the IsChecked property in the ToggleButton. You would need to use a custom ValueConverter. Here is one I found online:

/// <summary>  /// WPF/Silverlight ValueConverter : Convert boolean to XAML Visibility/// </summary>  [ValueConversion(typeof(bool), typeof(Visibility))]public class VisibilityConverter : IValueConverter{  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  {    return (value != null && (bool)value) ? Visibility.Visible : Visibility.Collapsed;  }  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  {    Visibility visibility = (Visibility)value;    return (visibility == Visibility.Visible);  }}