本文介绍了ResourceDictonary WPF中eventsetter中ListviewItem的PreviewMouseLeftButtonDown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 应用程序使用主题文件(ResourceDictionary)和整个应用程序的样式和控件。在这里我需要添加  ListViewItem的EventSetter。Application uses Theme file (ResourceDictionary) with style for whole application and the controls. Here i need to add  EventSetter for ListViewItem.<ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="" /> </Style></ListView.ItemContainerStyle> 此事件未设置为该项,因为它具有style = ItemStyle" $ This event is not set to the item because it has the style=ItemStyle"<Style x:Key="ItemsStyle" TargetType="{x:Type ListViewItem}"> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 推荐答案 不完全了解您的问题试图解决的问题。Not fully understand what is your question try to solved. WPF元素一次只能应用一种样式。 如果你有 tyle 哪个应该继承另一个,你可以 尝试 BasedOn 属性,用于设置样式的默认值。 If you have a style which should inherit another one, You cantry the BasedOn property to set a default value for a style. <Style x:Key="ListViewItemBasedStyle" TargetType="ListViewItem"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="" /> </Style> <Style x:Key="ItemsStyle" BasedOn="{StaticResource ListViewItemBasedStyle}" TargetType="{x:Type ListViewItem}"> <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>关于WPF中的样式,您可以看到: https://docs.microsoft.com/en-us/dotnet/api/system.windows.style。 basedon?view = netframework-4.7.1 About Style in WPF, You can see: https://docs.microsoft.com/en-us/dotnet/api/system.windows.style.basedon?view=netframework-4.7.1如果我有任何误解,也许你可以改写你的问题并给我们更多的背景。 If I have any misunderstanding, maybe you can rephrase your question and give us more context.此致 Bob 这篇关于ResourceDictonary WPF中eventsetter中ListviewItem的PreviewMouseLeftButtonDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 19:48