returning value item (not text item) in combobox.text [wpf C#] -
i have created custom class comboboxitem
takes text/value
, function return string.
public class comboboxitem { public string text { get; set; } public object value { get; set; } public override string tostring() { return text; } }
i populate item , added combobox
.
comboboxitem item = new comboboxitem(); item.text = result.codealias + " | " + result.description; item.value = result.codealias; combobox.items.add(item);
and combobox
shows both codealias + description
in each item. i'm trying return codealias
in content of box once user selects item instead of whole text
i tried following on selectionchanged
of combobox
.
combobox.text = (combobox.selecteditem comboboxitem).value.tostring();
and
combobox.selecteditem = (combobox.selecteditem comboboxitem).value.tostring();
and program crashes error:
object reference not set instance of object.
i put breakpoint
, gets value doesn't set it. ideas how this?
item datacontext
set viewsource
since need load initial value table. here's xaml:
<combobox x:name="combobox" selectionchanged="combobox_selectionchanged" datacontext="{staticresource mytableviewsource}" text="{binding myfield}" shell:windowchrome.ishittestvisibleinchrome="true" canvas.left="58" canvas.top="192" width="120"/>
not smoothest ui experience, can swap displaymemberpath between text , value when combobox's dropdown opened/closed. meets criteria.
xaml
<combobox x:name="mycombobox" dropdownopened="mycombobox_dropdownopened" dropdownclosed="mycombobox_dropdownclosed" />
codebehind
private void mycombobox_dropdownopened(object sender, eventargs e) { mycombobox.displaymemberpath = "text"; } private void mycombobox_dropdownclosed(object sender, eventargs e) { mycombobox.displaymemberpath = "value"; }
Comments
Post a Comment