WPF - why does this EventTrigger not work?

I am trying to get one style that fulfills all my triggers. This is what I have, but SoundPlayerAction does not fire:

<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Border.MouseDown">
                            <SoundPlayerAction Source="/sounds/simpleclick.wav"/>
                        </EventTrigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>                                
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>                                   
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
+3
source share
1 answer

I tested this XAML and the only problem is that your RoutedEvent is not starting. Border.MouseDown is definitely not going to work. If you want to play a sound when an item is selected (this is what you are trying to do), try the GotFocus event.

So modify your EventTrigger as follows:

<EventTrigger RoutedEvent="ListBoxItem.GotFocus">

For me it worked and the sound began to play.

+17
source

Source: https://habr.com/ru/post/1714315/


All Articles