c# - Add combobox text to its Itemssource -
i have combobox binded table called tenderness
through mvvm. i'm using entity framework.it displays records properly, need add functionality it. suppose user types in text not contained inside combobox's itemssource, want able add directly table , update itemssource well. have been able without mvvm, want know, how achieve using mvvm.
just did in lostfocus
event handler in setter of source property bind text
property of combobox
.
view model:
public observablecollection<string> items { get; } = new observablecollection<string>() { "a", "b", "c" }; private string _text; public string text { { return _text; } set { _text = value; onpropertychanged(nameof(text)); //add missing value... if (!items.contains(_text)) items.add(_text); } } private string _selecteditem; public string selecteditem { { return _selecteditem; } set { _selecteditem = value; onpropertychanged(nameof(selecteditem)); } }
view:
<combobox iseditable="true" text="{binding text, updatesourcetrigger=lostfocus}" itemssource="{binding items}" selecteditem="{binding selecteditem}" />
Comments
Post a Comment