1

我有一个菜单项和上下文菜单样式的默认样式,如下所示:

<ResourceDictionary>
    <Style TargetType="{x:Type MenuItem}">
         <Setter Property="Background" Value="Blue"/>
         <Setter Property="Foreground" Value="White" />
    </Style>
    <Style TargetType="{x:Type ContextMenu}">
        <Setter Property="Background" Value="LightBlue"/>
    </Style> 
</ResourceDictionary>

在绑定到可观察集合的列表视图项上显示上下文菜单时,我需要覆盖样式如下:

<ListView.Resources>
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="Black" />
    </Style>
    <Style TargetType="{x:Type ContextMenu}">
        <Setter Property="Background" Value="WhiteSmoke"/>
    </Style>
    <ContextMenu x:Key="ItemContextMenu" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=ContMenu}"/>
</ListView.Resources>
<ListView.ItemContainerStyle>
    <Style BasedOn="{StaticResource ListItemsStretched}" TargetType="{x:Type ListViewItem}">
        <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
    </Style>
</ListView.ItemContainerStyle>

这工作正常,直到我收到我在绑定的 ObservableCollection 中创建的事件以在任何属性发生更改时通知我,使用它以便我可以刷新 ICollectionView,这将导致列表视图的当前排序保持真实。但是,如果发生此事件时在列表视图上打开上下文菜单,它将恢复为默认样式而不是覆盖样式。

任何帮助将不胜感激?我怀疑这是我忽略的简单事情。

4

0 回答 0