c# - How to disable ItemsSource synchronization with Text property of ComboBox -
i use combobox binding string property of view model. choose combobox instead of textbox, because want have option choose list (as suggestion), don't want change selected text if itemssource changes.
i tried set issynchronizedwithcurrentitem property false, when list of suggestions change (at position of selected text), text changes empty. seems combobox has remembered entered text in list , when item disappears, text property cleared.
so question is: bug, or doing wrong? if bug, suggest work around?
i created sample project preproduces this:
in xaml:
<window x:class="testproject1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <combobox issynchronizedwithcurrentitem="false" itemssource="{binding items}" iseditable="true" text="{binding selectedtext, updatesourcetrigger=propertychanged}" horizontalalignment="left" margin="10,39,0,0" verticalalignment="top" width="120"/> <button click="button_click" content="update list" horizontalalignment="left" margin="10,82,0,0" verticalalignment="top" width="75"/> </grid> </window>
in code behind:
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); this.datacontext = this; items = new list<string>() { "0", "1", "2" }; } public event propertychangedeventhandler propertychanged; private void raisepropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } private list<string> _items; public list<string> items {// use ienumerable<string> linq, effect same { return _items; } set { if (_items != value) { _items = value; raisepropertychanged("items"); } } } private string _selectedtext; public string selectedtext { { return _selectedtext; } set { if (_selectedtext != value) { _selectedtext = value; raisepropertychanged("selectedtext"); } } } private void button_click(object sender, routedeventargs e) { var changed = items.tolist();//clone int index = changed.indexof(selectedtext); if (index >= 0) { changed[index] += "a";//just change selected value } items = changed;//update new list } }
after itemssource has changed, raise property changed on selected text refresh ui.
so in items collection setter, make change:
raisepropertychanged("items"); raisepropertychanged("selectedtext");
edit: in example aren't changing itemsource, changing text of item selected 1 having text binding on old text. expecting see/happen? wanting selected item stay same, if text changes?
Comments
Post a Comment