c# - WPF Deleting item from Combobox when hovering mouse over it (MVVM) -


i have combobox binded observeable collection. collection container selfdefined class.

i need delete arbitary item combobox pressing right mouse button when hovering mouse cursor on item of dropdown list. need delete pressing delete button when item highlighted.

i have solution in code behind, need mvvm-pattern.

can me in issue pls?

thx in advance :).

here codes:

my viewmodel:

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         /// <summary>         /// initializes new instance of <see cref="mainwindowvm"/> class.         /// </summary>         public mainwindowvm()         {             previewkeydowncmd = new command<keyeventargs>(previewkeydowncmdexecute);             personlist = new observablecollection<person>();             personlist.add(new person("aa"));             personlist.add(new person("bb"));         }         #endregion           #region properties         /// <summary>         /// gets or sets property value.         /// </summary>         public observablecollection<person> personlist         {             { return getvalue<observablecollection<person>>(personlistproperty); }             set { setvalue(personlistproperty, value); }         }          /// <summary>         /// register personlist property known in class.         /// </summary>         public static readonly propertydata personlistproperty =              registerproperty("personlist", typeof(observablecollection<person>), null);         #endregion           #region commands         /// <summary>         /// gets previewkeydowncmd command.         /// </summary>         public command<keyeventargs> previewkeydowncmd { get; private set; }          /// <summary>         /// method invoke when previewkeydowncmd command executed.         /// </summary>         private void previewkeydowncmdexecute(keyeventargs e)         {             if (e.key == key.delete)             {                 //********************what should here?***************************             }         }         #endregion     } } 

xaml file:

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

person class:

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          /// <summary>         /// gets or sets property value.         /// </summary>         [model]         public string name         {             { return getvalue<string>(nameproperty); }             private set { setvalue(nameproperty, value); }         }          /// <summary>         /// register name property known in class.         /// </summary>         public static readonly propertydata nameproperty =              registerproperty("name", typeof(string));          public override string tostring()         {             return name;         }     } } 

solution in codebehind in non mvvm-project:

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) comboboxitem;                  if (cbi.ishighlighted == true)                 {                     peoples.remove(item);                     return;                 }             }         }     } 

the easiest way create property selectedperson. user right-clicks on item, automatically set selectedperson. can create toolbar uses same command popup delete selected item in list.

when use selectedperson method, can use code this:

mycollection.remove(selectedperson); selectedperson = null; 

make sure in oncanexecute, check whether selectedperson != null.


Comments