在WPF中不处于焦点时,如何从ListView中删除所选内容?

在WPF中不处于焦点时,如何从ListView中删除所选内容?,wpf,Wpf,我的WPF UserControl中有多个ListView和ListBox。我想在选定的项目不在焦点时将其删除 我试过以下方法 <Window.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="White"/> </Window.Resources> 上述方法没有区别 <Style Tar

我的WPF UserControl中有多个ListView和ListBox。我想在选定的项目不在焦点时将其删除

我试过以下方法

<Window.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="White"/>
</Window.Resources>

上述方法没有区别

<Style TargetType="ListViewItem">
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="false">
             <Setter Property="Selector.IsSelected" Value="False"/>
         </Trigger>
    </Style.Triggers>
 </Style>


此方法仅在选择一个项目时删除选择。如果SelectionMode=“Extended”,此方法不起作用。

我建议使用以下代码:

<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding Item}"  
          FocusLost="your ICommand implementation/>

我想应该对你有用。

考虑写一个附加的行为来实现这一点。与使用触发器相比,您将拥有更多的控制。是否希望删除您的选择,然后在usercontrol获得焦点时重新应用?还是刚刚移除?@netaholic我想完全移除它。我就是这么做的。但是我想知道我是否可以为所有控件应用一个通用样式。您所说的通用样式是什么意思?如果答案正确,请将其标记为答案(在左侧)。这是直接的方法。我在问题中提到,我有多个ListView和ListBox。我想知道我是否可以对所有这些控件应用一种通用样式来实现我所需的功能
try { 
  Items.Remove(Item); 
} catch (Exception ex) { 
  ...
}