如果 ItemContainerStyle 为 style,则 EventSetter 不会为 ListView 触发
相对于 WPF 的 n00b。我有一个 ListView
因此:
<ListView>
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="ItemDoubleClick"/>
</Style>
</ListView.Resources>
</ListView>
在我的 app.xaml 中,我有以下样式:
<Style TargetType="{x:Type ListView}">
<Setter Property="ItemContainerStyle" Value="{DynamicResource ListViewItemStyle}"/>
</Style>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="Border" Padding="4">
<GridViewRowPresenter x:Name="ItemText"
TextBlock.FontSize="14" TextBlock.Foreground="{x:Static SystemColors.ControlDarkDarkBrush}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter TargetName="ItemText" Property="TextBlock.Foreground" Value="{x:Static SystemColors.WindowTextBrush}"/>
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.HighlightBrush}"/>
<Setter TargetName="ItemText" Property="TextBlock.Foreground" Value="{x:Static SystemColors.HighlightTextBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但是一旦我设置 ItemContainerStyle
,双击就不再触发。如果我删除它,它会触发,但我的 ListViewItem
没有样式。
我在这里缺少什么?
A relative n00b to WPF. I have a ListView
thus:
<ListView>
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="ItemDoubleClick"/>
</Style>
</ListView.Resources>
</ListView>
And in my app.xaml I have the following styles:
<Style TargetType="{x:Type ListView}">
<Setter Property="ItemContainerStyle" Value="{DynamicResource ListViewItemStyle}"/>
</Style>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="Border" Padding="4">
<GridViewRowPresenter x:Name="ItemText"
TextBlock.FontSize="14" TextBlock.Foreground="{x:Static SystemColors.ControlDarkDarkBrush}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter TargetName="ItemText" Property="TextBlock.Foreground" Value="{x:Static SystemColors.WindowTextBrush}"/>
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.HighlightBrush}"/>
<Setter TargetName="ItemText" Property="TextBlock.Foreground" Value="{x:Static SystemColors.HighlightTextBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But once I'm setting the ItemContainerStyle
, the double-click no longer fires. If I remove it, it fires but my ListViewItem
s are not styled.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的本地资源被应用程序资源样式覆盖,这会更改 ListView 的 ItemContainerStyle 属性。我建议直接在
ListView.ItemContainerStyle
上设置样式,并使样式基于现有样式:(这假定隐式样式,因此要么删除应用程序资源中样式的键,要么使用
BaseOn
属性中的该键直接引用它)Your local resource is overridden by the application resources style which changes the ListView's ItemContainerStyle property. I would suggest setting the style directly on the
ListView.ItemContainerStyle
and basing the style on the existing one:(This assumes implicit styling, so either remove the key of the style in your application resources or reference it directly using that key in the
BasedOn
property)为了扩展 HB 的答案,元素可以具有隐式 Style,也可以直接设置它的 Style 属性(我称之为显式 Style),但不能同时设置两者。一旦您在 ListViewItem 上设置 Style 属性,它将不再使用您拥有的任何隐式样式。
由于 ListView.ItemContainerStyle 只是设置 ListViewItem.Style 属性的简单方法,因此它具有与您定义的隐式 Style 短路相同的效果。
To expand on H.B.'s answer, an element can either have an implicit Style or you can set it's Style property directly (what I call an explicit Style), but not both. As soon as you set the Style property on say a ListViewItem, it will no longer use any implicit Styles you have.
Since the ListView.ItemContainerStyle is just an easy way to set the ListViewItem.Style property, it has the same effect of short-circuiting the implicit Style you have defined.