MVVM WPF - ComboBox two way binding inside ItemsControl -
i working on problem day now.
for reason unable twoway bind value combobox if inside itemscontrol. outside works fine.
i have observablecollection of int? in viewmodel:
private observablecollection<int?> _sorterexitssettings = new observablecollection<int?>(); public observablecollection<int?> sorterexitssettings { { return _sorterexitssettings; } set { if (_sorterexitssettings != value) { _sorterexitssettings = value; raisepropertychanged("sorterexitssettings"); } } }
my xaml:
<itemscontrol itemssource="{binding sorterexitssettings}"> <itemscontrol.itemtemplate> <datatemplate> <combobox itemssource="{binding relativesource={relativesource ancestortype=itemscontrol}, path=datacontext.scanrouter.stores}" selectedvalue="{binding path=., mode=twoway, updatesourcetrigger=propertychanged}" displaymemberpath="name" selectedvaluepath="id" iseditable="true" /> </datatemplate> </itemscontrol.itemtemplate>
so combobox populated list of stores. works fine far. observablecollection sorterexitssettings has values set shown in displayed comboboxes. setting selectedvalue works.
however when change selection, sorterexitssettings wont change. while when implement comboboxes(100) without itemscontrol works fine.
<combobox itemssource="{binding scanrouter.stores}" displaymemberpath="name" selectedvaluepath="id" iseditable="true" selectedvalue="{binding sorterexitssettings[0], mode=twoway, updatesourcetrigger=propertychanged}"/>
even better when implement comboboxes using itemscontrol , example combobox shown above. when change single combobox's value change value of combobox inside itemscontrol, not other way around.
did encounter problem before?
my guess itemscontrol doesn't fact binding selected value item in list. when bind directly viewmodel property(store) doesn't work. tried using selcteditem instead of selectedvalue , populate observablecollection store objects instead of int?.
the problem you're binding combobox's selectedvalue directly collection elements type int ?
. won't work, binding targets have properties. try wrapping int ?
values in class , expose value property of class getter , setter, i.e. this:
private observablecollection<wrapper> _sorterexitssettings = new observablecollection<wrapper>(); ... etc...
and:
public class wrapper { public int? value {get; set;} }
and finally:
<combobox ... selectedvalue="{binding path=value, mode=twoway...
post here if still have problems.
Comments
Post a Comment