title: WPF Deleting item from Combobox when hovering mouse over it (MVVM)
body: <p>I have a combobox binded to a Observeable Collection. The Collection is a container for a selfdefined class.</p>
  
  <p>I need to delete an arbitary item from the combobox by pressing the right mouse button when hovering my mouse cursor over the item of the dropdown list. I also need to delete it by pressing the delete button when the item is highlighted.</p>
  
  <p>I already have a solution in the code behind, but i need to do it with the MVVM-Pattern.</p>
  
  <p>Can anyone help me in this issue pls?</p>
  
  <p>Thx in advance :).</p>
  
  <p>Here are my codes:</p>
  
  <p>My ViewModel:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  using System.Text;
  using Catel.MVVM;
  using System.Windows.Input;
  using DeleteItemFromComboBox.Models;
  using Catel.Data;
  
  namespace DeleteItemFromComboBox.ViewModels
  {
      public class MainWindowVM : ViewModelBase
      {
  
          #region Constructors
          /// &lt;summary&gt;
          /// Initializes a new instance of the &lt;see cref="MainWindowVM"/&gt; class.
          /// &lt;/summary&gt;
          public MainWindowVM()
          {
              PreviewKeyDownCmd = new Command&lt;KeyEventArgs&gt;(PreviewKeyDownCmdExecute);
              PersonList = new ObservableCollection&lt;Person&gt;();
              PersonList.Add(new Person("AA"));
              PersonList.Add(new Person("BB"));
          }
          #endregion
  
  
          #region Properties
          /// &lt;summary&gt;
          /// Gets or sets the property value.
          /// &lt;/summary&gt;
          public ObservableCollection&lt;Person&gt; PersonList
          {
              get { return GetValue&lt;ObservableCollection&lt;Person&gt;&gt;(PersonListProperty); }
              set { SetValue(PersonListProperty, value); }
          }
  
          /// &lt;summary&gt;
          /// Register the PersonList property so it is known in the class.
          /// &lt;/summary&gt;
          public static readonly PropertyData PersonListProperty = 
              RegisterProperty("PersonList", typeof(ObservableCollection&lt;Person&gt;), null);
          #endregion
  
  
          #region Commands
          /// &lt;summary&gt;
          /// Gets the PreviewKeyDownCmd command.
          /// &lt;/summary&gt;
          public Command&lt;KeyEventArgs&gt; PreviewKeyDownCmd { get; private set; }
  
          /// &lt;summary&gt;
          /// Method to invoke when the PreviewKeyDownCmd command is executed.
          /// &lt;/summary&gt;
          private void PreviewKeyDownCmdExecute(KeyEventArgs e)
          {
              if (e.Key == Key.Delete)
              {
                  //********************What Should i do here?***************************
              }
          }
          #endregion
      }
  }
  </code></pre>
  
  <p>XAML File:</p>
  
  <pre><code>&lt;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"&gt;
  
  &lt;Window.Resources&gt;
      &lt;ViewModels:MainWindowVM x:Key="ViewModel"/&gt;
  &lt;/Window.Resources&gt;
  
  &lt;Grid DataContext="{Binding Source={StaticResource ViewModel}}"&gt;
      &lt;ComboBox Height="44" 
                HorizontalAlignment="Left" 
                Margin="12,12,0,0" 
                Name="comboBox1" 
                VerticalAlignment="Top" 
                Width="479"
                ItemsSource="{Binding PersonList, Mode=TwoWay}" &gt;
  
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="PreviewKeyDown"&gt;
                  &lt;catel:EventToCommand Command="{Binding PreviewKeyDownCmd}" DisableAssociatedObjectOnCannotExecute="False" /&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
  
      &lt;/ComboBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Person Class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Catel.MVVM;
  using Catel.Data;
  using System.Runtime.Serialization;
  
  namespace DeleteItemFromComboBox.Models
  {
  
  #if !SILVERLIGHT
      [Serializable]
  #endif
      public class Person : ModelBase
      {
  
          #region Constructors
  
          public Person() { }
  
          public Person(string name)
          {
              this.Name = name;
          }
  
  #if !SILVERLIGHT
          protected Person(SerializationInfo info, StreamingContext context)
              : base(info, context) { }
  #endif
          #endregion
  
          /// &lt;summary&gt;
          /// Gets or sets the property value.
          /// &lt;/summary&gt;
          [Model]
          public string Name
          {
              get { return GetValue&lt;string&gt;(NameProperty); }
              private set { SetValue(NameProperty, value); }
          }
  
          /// &lt;summary&gt;
          /// Register the Name property so it is known in the class.
          /// &lt;/summary&gt;
          public static readonly PropertyData NameProperty = 
              RegisterProperty("Name", typeof(string));
  
          public override string ToString()
          {
              return Name;
          }
      }
  }
  </code></pre>
  
  <p>Solution in Codebehind in a non MVVM-Project:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void comboBox1_PreviewKeyDown(object sender, KeyEventArgs e)
      {
          if (e.Key == Key.Delete)
          {
              foreach (People item in comboBox1.Items)
              {
                  ComboBoxItem cbi = this.comboBox1.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
  
                  if (cbi.IsHighlighted == true)
                  {
                      peoples.Remove(item);
                      return;
                  }
              }
          }
      }
  </code></pre>