c# - Caliburn resetting SelectedIndex on ComboBox -
i have combobox has itemssource
bound list of items in viewmodel selecteditem
being bound property. have combobox bound list, uses selectedindex
instead. when select item first combobox, changes contents of seconds combobox, , property bound selectedindex
gets set -1
, causes nothing selected in combobox.
why selectedindex
property reset -1, , can prevent it?
view
<combobox itemssource="{binding mylist}" selecteditem="{binding myselecteditem}"></combobox> <combobox itemssource="{binding myarray}" selectedindex="{binding myselectedindex}"></combobox>
viewmodel
public list<foo> mylist { get; set; } private foo _myselecteditem; public foo myselecteditem { { return _myselecteditem; } set { if (equals(value, _myselecteditem)) return; _myselecteditem = value; notifyofpropertychange(); myarray = new [] { "othervalue1", "othervalue2", "othervalue3" }; notifyofpropertychange(() => myarray); } } public string[] myarray { get; set; } public int myselectedindex { get; set; } public myviewmodel() { mylist = new list<foo> { new foo(), new foo(), new foo() }; myselecteditem = mylist.first(); myarray = new [] { "value1", "value2", "value3" }; myselectedindex = 1; // "value2" notifyofpropertychange(() => mylist); }
so, selecting combobox bound mylist causes myarray built new values. causes myselectedindex have value -1
, though same indices exist in new array.
the selecteditem
indeed reset because selected item cleared out when itemssource
property set new collection of items.
but should able store index in temporary variable , re-assign after itemssource
has been updated:
public foo myselecteditem { { return _myselecteditem; } set { if (equals(value, _myselecteditem)) return; _myselecteditem = value; notifyofpropertychange(); int temp = myselectedindex; myarray = new[] { "othervalue1", "othervalue2", "othervalue3" }; notifyofpropertychange(() => myarray); selectedindex = temp; notifyofpropertychange(() => selectedindex); } }
Comments
Post a Comment