如果 ItemContainerStyle 为 style,则 EventSetter 不会为 ListView 触发

发布于 2024-12-01 05:45:53 字数 2526 浏览 2 评论 0原文

相对于 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 ListViewItems are not styled.

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-12-08 05:45:53

您的本地资源被应用程序资源样式覆盖,这会更改 ListView 的 ItemContainerStyle 属性。我建议直接在 ListView.ItemContainerStyle 上设置样式,并使样式基于现有样式:(

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}"
           BasedOn="{StaticResource {x:Type ListViewItem}}">
        <EventSetter Event="MouseDoubleClick" Handler="ItemDoubleClick"/>
    </Style>
</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:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}"
           BasedOn="{StaticResource {x:Type ListViewItem}}">
        <EventSetter Event="MouseDoubleClick" Handler="ItemDoubleClick"/>
    </Style>
</ListView.ItemContainerStyle>

(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)

夜空下最亮的亮点 2024-12-08 05:45:53

为了扩展 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文