MouseLeftButtonDown does not work on Border inside a Style

285 Views Asked by At

I'm using a MouseLeftButtonDown on a Border inside a Style. I get this error:

The event 'MouseLeftButtonDown' cannot be specified on a Target tag in a Style. Use an EventSetter instead.

This is my xaml:

<ItemsControl.Style>
                    <Style TargetType="{x:Type ItemsControl}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Items.Count,
                RelativeSource={RelativeSource Self}}"  Value="0">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Border BorderThickness="2" BorderBrush="Black" CornerRadius="2" Padding="10" Margin="10" MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown" >
                                                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" >Keine Dateien angehängt</TextBlock>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
            </ItemsControl.Style>

How to fix this?

1

There are 1 best solutions below

0
On BEST ANSWER

How to fix this?

You could for example define a Style for the Border as a separate resource:

<ItemsControl>
    <ItemsControl.Resources>
        <Style x:Key="BorderStyle" TargetType="Border">
            <EventSetter Event="MouseLeftButtonDown" Handler="UIElement_MouseLeftButtonDown" />
        </Style>
    </ItemsControl.Resources>
    <ItemsControl.Style>
        <Style TargetType="{x:Type ItemsControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}"  Value="0">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border BorderThickness="2" BorderBrush="Black" CornerRadius="2" Padding="10" Margin="10"
                                                Style="{StaticResource BorderStyle}">
                                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" >Keine Dateien angehängt</TextBlock>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.Style>
</ItemsControl>