C# WPF将鼠标悬停在组合框上时从组合框中删除项目(MVVM)

C# WPF将鼠标悬停在组合框上时从组合框中删除项目(MVVM),c#,wpf,mvvm,combobox,catel,C#,Wpf,Mvvm,Combobox,Catel,我有一个组合框绑定到一个可观的收藏。集合是自定义类的容器 当鼠标光标悬停在下拉列表的项目上时,我需要通过按下鼠标右键从组合框中删除任意项目。当项目突出显示时,我还需要按delete按钮将其删除 我已经在代码隐藏中找到了一个解决方案,但我需要使用MVVM模式来实现它 有人能帮我解决这个问题吗 Thx预付款:) 这是我的密码: 我的ViewModel: 使用系统; 使用System.Collections.Generic; 使用System.Collections.ObjectModel; 使用Sy

我有一个组合框绑定到一个可观的收藏。集合是自定义类的容器

当鼠标光标悬停在下拉列表的项目上时,我需要通过按下鼠标右键从组合框中删除任意项目。当项目突出显示时,我还需要按delete按钮将其删除

我已经在代码隐藏中找到了一个解决方案,但我需要使用MVVM模式来实现它

有人能帮我解决这个问题吗

Thx预付款:)

这是我的密码:

我的ViewModel:

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用Catel.MVVM;
使用System.Windows.Input;
使用DeleteItemFromComboBox.Models;
使用类别数据;
命名空间DeleteItemFromComboBox.ViewModels
{
公共类MainWindowVM:ViewModelBase
{
#区域构造函数
/// 
///初始化类的新实例。
/// 
公共MainWindowVM()
{
PreviewKeyDownCmd=新命令(PreviewKeyDownCmdExecute);
PersonList=新的ObservableCollection();
人员列表。添加(新人员(“AA”);
人员列表。添加(新人员(“BB”);
}
#端区
#区域属性
/// 
///获取或设置属性值。
/// 
公共观察收集人员
{
获取{返回GetValue(PersonListProperty);}
set{SetValue(PersonListProperty,value);}
}
/// 
///注册PersonList属性,使其在类中已知。
/// 
公共静态只读属性Data PersonListProperty=
RegisterProperty(“PersonList”,typeof(ObservableCollection),null;
#端区
#区域命令
/// 
///获取PreviewKeyDownCmd命令。
/// 
公共命令PreviewKeyDownCmd{get;private set;}
/// 
///方法在执行PreviewKeyDownCmd命令时调用。
/// 
私有void PreviewKeyDownCmdExecute(KeyEventArgs e)
{
if(e.Key==Key.Delete)
{
//********************我在这里该怎么办***************************
}
}
#端区
}
}
XAML文件:

<Window x:Class="DeleteItemFromComboBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModels="clr-namespace:DeleteItemFromComboBox.ViewModels"
    Title="MainWindow" Height="350" Width="525"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:catel="http://catel.codeplex.com">

<Window.Resources>
    <ViewModels:MainWindowVM x:Key="ViewModel"/>
</Window.Resources>

<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
    <ComboBox Height="44" 
              HorizontalAlignment="Left" 
              Margin="12,12,0,0" 
              Name="comboBox1" 
              VerticalAlignment="Top" 
              Width="479"
              ItemsSource="{Binding PersonList, Mode=TwoWay}" >

        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewKeyDown">
                <catel:EventToCommand Command="{Binding PreviewKeyDownCmd}" DisableAssociatedObjectOnCannotExecute="False" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

    </ComboBox>
</Grid>

最简单的方法是创建属性SelectedPerson。一旦用户右键单击某个项目,它将自动设置SelectedPerson。然后,您还可以创建一个工具栏,该工具栏使用与弹出窗口相同的命令删除列表中的选定项

使用SelectedPerson方法时,可以使用如下代码:

MyCollection.Remove(SelectedPerson);
SelectedPerson = null;

确保在OnCanExecute中,检查SelectedPerson!=空。

我使用以下方法之一来解决问题:

  • 将MouseButtonEventArgs e.Source强制转换为Combobox,然后应用上面提到的解决方案来完成任务。只需使用您选择的类型删除IPlan

    private void Plan_PreviewMouseRightButtonDownCmd_Execute(MouseButtonEventArgs e)
    {
        ComboBox comboBox =  e.Source as ComboBox;
        if(comboBox!=null)
        {
            foreach (IPlan item in comboBox.Items)
            {
                ComboBoxItem cbi = comboBox.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
    
                if (cbi.IsHighlighted == true)
                    SelectedPlans.Remove(item);
    
                if (item == SelectedPlan)
                    SelectedPlan = null;
            }
        }
    }
    
  • 使用此combobox的自定义实现,并将itemindex绑定到代码隐藏中的属性。然后可以通过MyCollection.RemoveAt()删除该项


  • 我认为在这个
    ((ComboBoxItem)cbi中不需要演员阵容。IsHighlighted
    ,只要
    cbi。IsHighlighted
    就可以了。是的,谢谢你的提示:)。嗨,Geert,谢谢你的快速回答。这可能是一个非常好的替代解决方案,但不幸的是,我需要实现上述询问功能,因为selectedperson绑定到selectedchanged事件,并且系统仅在所选项目更改时更新selectedperson。当所选项目发生更改时,我的程序需要处理其他任务。顺便说一句,您的CATEL框架非常成熟,支持非常好:)。我喜欢它。继续做好工作:)。