Wpf CollectionViewSource取消选择单击组名时选择编辑项

Wpf CollectionViewSource取消选择单击组名时选择编辑项,wpf,xaml,collectionviewsource,Wpf,Xaml,Collectionviewsource,我有一个listbox,它的itemSource绑定到一个collectionViewSource,该列表框被分组,并且在实际项目上有两个级别的分组: <ListBox ItemsSource="{Binding Source={StaticResource myCVS}}" ItemTemplate="{StaticResource myItemsTemplate}" ItemContainerStyle="{StaticResource myItemsStyle}" S

我有一个
listbox
,它的
itemSource
绑定到一个
collectionViewSource
,该列表框被分组,并且在实际项目上有两个级别的分组:

        <ListBox ItemsSource="{Binding Source={StaticResource myCVS}}" ItemTemplate="{StaticResource myItemsTemplate}" ItemContainerStyle="{StaticResource myItemsStyle}" SelectedItem="{Binding SelectedListItem}" >
            <ListBox.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource HeaderStyle}" />
                <GroupStyle ContainerStyle="{StaticResource SubHeaderStyle}" />
            </ListBox.GroupStyle>
        </ListBox>
observableCollection
中的项目如下所示:

public class Items
{
    public string GroupName;
    public string SubGroupName;
    public string ItemName;
}
这一切都很好,我最终得到了:

Header1
 |_SubHeader1
     |_item1
     |_item2
Header2
 |_SubHeader2
     |_item1
     |_item2

问题是,如果单击某个项目,它将变为选中状态,如果单击标题或子标题,它将保持选中状态。如果单击标题,我希望将
SelectedItem
设置为空。我正在使用命令从UI中删除
SelectedItem
,但如果仅在单击项目时单击标题或子标题,我不希望执行该命令

GroupStyle
s是不可选择的,因此您的视图模型当然不会看到发生选择更改

为了解决这个问题,您可以使用一些代码隐藏。您会注意到,如果单击
ListBox
中的项目,则
ListBoxItem
会将MouseUp事件的
Handled
属性设置为true。如果单击
列表框
上的任何其他位置,则不会处理该事件。话虽如此,您可以根据
Handled
的状态设置所选项目

XAML:

<ListBox ItemsSource="{Binding Source={StaticResource myCVS}}"
         ItemTemplate="{StaticResource myItemsTemplate}"
         ItemContainerStyle="{StaticResource myItemsStyle}"
         SelectedItem="{Binding SelectedListItem}"
         MouseLeftButtonUp="ListBox_MouseLeftButtonUp">
private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if(!e.Handled)
    {
        var lb = sender as ListBox;
        lb.SelectedItem = null;
    }
}
附录:

<ListBox ItemsSource="{Binding Source={StaticResource myCVS}}"
         ItemTemplate="{StaticResource myItemsTemplate}"
         ItemContainerStyle="{StaticResource myItemsStyle}"
         SelectedItem="{Binding SelectedListItem}"
         MouseLeftButtonUp="ListBox_MouseLeftButtonUp">
private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if(!e.Handled)
    {
        var lb = sender as ListBox;
        lb.SelectedItem = null;
    }
}
单击已选择的项目将SelectedItem设置为空。要防止出现这种情况,请执行以下操作:不要使用
MouseLeftButtonUp
use
MouseDown:

<ListBox ItemsSource="{Binding Source={StaticResource myCVS}}"
         SelectedItem="{Binding SelectedListItem}"
         MouseDown="ListBox_MouseLeftButtonUp">


当前应用程序(
GroupStyle
)的状态无法正确绘制,但实现在这里很重要。如果这不适合您,我将实现一个纯MVVM方法。

谢谢,这很有效,我只需要稍微调整一下。这正是我想要的,谢谢@好的。如果有资格,别忘了奖励奖金;)也许你可以帮我做另一件事。鼠标向下(在列表中高亮显示)时项目被选中鼠标向上(在所选项目上)时高亮显示消失。我可以通过将所选项目背景色和selectedItemInactive背景色都设置为透明来解决这个问题。我的所有其他代码(命令)仍然有效,但我希望在视觉上突出显示所选项目。并且仅在单击groupstle时将其设置为null。有什么想法吗?看来项目上的鼠标向上事件也由代码隐藏中的此事件处理程序处理,因此无论单击什么,e.handled都是false。项目或团体风格。我想我可能需要在活动中检测它是否是团体风格。