c# - WPF - combobox items is getting duplicated -


i'm creating wpf application , in settings panel have couple of labels, textboxes, comboboxes , 2 buttons (save) , (cancel).

xaml

<combobox x:name="mycombobox" grid.column="1" margin="18,372,4,0" verticalalignment="top" height="26" selectionchanged="mycombobox_selectionchanged" /> 

i have added items combobox :

mycombobox.items.add("test1"); mycombobox.items.add("test2"); mycombobox.items.add("test3"); foreach (var item in mycombobox.items)     if (item.equals(properties.settings.default.mycombobox))         mycombobox.selecteditem = item; 

and added selectionchanged event. how looks:

private void mycombobox_selectionchanged(object sender, selectionchangedeventargs e) {     if (mycombobox.selecteditem.tostring().equals("test1"))     {         testgrid.visibility = visibility.visible;     }     else if (mycombobox.selecteditem.tostring().equals("test2") || mycombobox.selecteditem.tostring().equals("test3"))     {         testgrid.visibility = visibility.hidden;     } } 

when click cancel button , restart settings panel items of combobox duplicated. (same values twice).

i have tried prevent adding cancel buttons click event

mycombobox.items.clear(); 

but @ point problem exists (mycombobox.selecteditem null) , error:

an exception of type 'system.nullreferenceexception' occurred in identificationstation.exe not handled in user code

how prevent comboboxes items duplicated? or should mycombobox_selectionchanged otherwice , helps?

can't avoid

'system.nullreferenceexception'

by testing if mycombobox.selecteditem null on mycombobox_selectionchanged?

private void mycombobox_selectionchanged(object sender, selectionchangedeventargs e) {     if (mycombobox.selecteditem != null)         if (mycombobox.selecteditem.tostring().equals("test1"))         {             testgrid.visibility = visibility.visible;         }         else if (mycombobox.selecteditem.tostring().equals("test2") || mycombobox.selecteditem.tostring().equals("test3"))         {             testgrid.visibility = visibility.hidden;         }     } } 

i don't think it's bad manner that.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -