Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf-ICollectionView刷新导致样式覆盖失败_C#_Wpf_Icollectionview - Fatal编程技术网

C# Wpf-ICollectionView刷新导致样式覆盖失败

C# Wpf-ICollectionView刷新导致样式覆盖失败,c#,wpf,icollectionview,C#,Wpf,Icollectionview,我有菜单项的默认样式和上下文菜单样式,如下所示: <ResourceDictionary> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Background" Value="Blue"/> <Setter Property="Foreground" Value="White" /> </Style> <

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

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

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

<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,从而使listview的当前排序保持为true。但是,如果此事件发生时列表视图上的关联菜单处于打开状态,它将恢复为默认样式,而不是替代样式

在这方面有什么帮助吗?我怀疑我忽略了一些简单的事情