0

我在样式内的边框上使用 MouseLeftButtonDown。我收到此错误:

无法在样式中的目标标记上指定事件“MouseLeftButtonDown”。请改用 EventSetter。

这是我的 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>

如何解决这个问题?

4

1 回答 1

2

如何解决这个问题?

例如,您可以将 a 定义StyleBorder单独的资源:

<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>
于 2020-02-05T12:30:23.483 回答