c# - ListBox selectedItem get working but set not working in MVVM -


i working on wpf application , following mvvm. in view there grid view contains different columns. 1 of these column listbox. problem listbox column, selecteditem works fine set doesn't.

here view code

<datagrid itemssource="{binding items}" selecteditem="{binding selecteditem}" selectionmode="single">     <datagrid.columns>         <datagridtextcolumn binding="{binding name}" header="name" />         <datagridtemplatecolumn header="actions">             <datagridtemplatecolumn.celltemplate>                 <datatemplate>                     <listbox displaymemberpath="name" itemssource="{binding actions}" selecteditem="{binding selectedaction}" />                 </datatemplate>             </datagridtemplatecolumn.celltemplate>         </datagridtemplatecolumn>     </datagrid.columns> </datagrid> 

in viewmodel, have main viewmodel class, contains list of items. item class contains name, list of actions , selected action.

public class myviewmodel : inotifyofpropertychanged {     private observablecollection<item> _items;     public observablecollection<item> items     {         { return _items?? (_items= new observablecollection<item>); }     }      private item _selecteditem;     public item selecteditem     {         { return _selecteditem; }         set { _selecteditem= value; }     } }  public class item : inotifyofpropertychanged {     public string name;      private observablecollection<string> _actions;     public observablecollection<string> actions     {         { return _actions?? (_actions= new observablecollection<string>); }     }      private string _selectedaction;     public string selectedaction     {         { return _selectedaction; }         set { _selectedaction = value; }     } } 

now selecteditem items list works fine. selecteditem insde item class actions doesn't work completely. inserted breakpoints on getter , setter of selectedaction. breakpoint hits. if select action ui set breakpoint selectedaction doesn't hit.

what's problem.

view

when select archive project or restore project, setter of selectedaction doesn't called.

note: have removed unnecessary information loading data in lists, implementation of inotifyofpropertychanged etc.

i don't know if case since you've said have cut information i'll try answer.

this happen me when add custum template listbox or listview , controls within listboxitem handle click event themselve.

for example if have radio button this

<listbox itemssource="{binding list}" selecteditem="{binding item}">   <listbox.itemtemplate>     <datatemplate>       <radiobutton content="{binding name}" groupname="groupname">     </datatemplate>   </listbox.itemtemplate> </listbox> 

the selecteditem not set when actualy click on radio button because radio button handle click event , not bubble listbox may change it's selection.

you have make sure listbox event , controls in template react accordingly. in case you'll have this.

<listbox itemssource="{binding list}" selecteditem="{binding item}">   <listbox.itemtemplate>     <datatemplate>       <radiobutton content="{binding name}" ishittestvisible="false">         <radiobutton.style>           <style targettype="{x:type radiobutton}">             <setter property="ischecked" value="{binding path=isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" />           </style>         </radiobutton.style>       </radiobutton>     </datatemplate>   </listbox.itemtemplate> </listbox> 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -