Click here to Skip to main content
13,152,454 members (37,271 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all

I faced some odd problem in my project.

In my application I initialize combobox with my classes. Works as expected.

foreach (MyClass c in Classes)

I have event in my app which handles those classes and I want combobox select automatically that class as selecteditem. When I debug my app it looks fine, right class goes into combobox, but selecteditem remains null and therefore ui shows empty combobox.

here is snippet of my event..
Event(object sender, EventArgs e)
myComboBox.SelectedItem = e.MyClass; //looks right when debugging, but doest work in Ui side

its odd that combobox cannot handle this because it has that class already initialized in it.

Hope you got idea :)

Posted 25-Feb-11 1:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In my opinion, you have to set the DataTemplate for that Combobox to display the data.

But you have to set the DataContext of the Combobox to your class.
Do this in you code-behind:


It goes like this:
<combobox name="myCombobox" itemssource="{Binding}">
<textblock text="{Binding Path=Yourproperty}" />

where "Yourproperty" is the property of that class whose value is to be displayed.

or you can also set the DisplayMemerBinding directly to the ComboBox :

<combobox name="Acombo" itemssource="{Binding}" displaymemberbinding="{Binding Path=Yourproperty}" />
Espen Harlinn 25-Feb-11 8:50am
Good reply, my 5
Tarun.K.S 25-Feb-11 8:53am
raistu 28-Feb-11 3:25am
Thank you for your help! I appreciate that you are not only pointing out of errors, but you also provided codesnippet to solve it. Perfect answer, you got my 5 :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Are you sure they are the same reference? A data-copy of the original instance won't be considered the same.
SAKryukov 25-Feb-11 21:40pm
This is the only reasonable explanation I can see, my 5.
(other Answers simply do not address or workaround the problem)
raistu 27-Feb-11 12:52pm
Hi there! I havent tested other answers yet, but I dont fully understand yours.. what you mean by referencing? Like I said when debugging my combobox holds right classes as expected, event gives known class to combobox, but still it doesnt react like I want to. If bossible could you provide code snippet?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You could also consider using a CollectionViewSource[^] to provide a CollectionView[^] and otherwise use the approach outlined by Tarun.K.S.

CollectionView has a CurrentItem[^] property that adds to the functionality of your view-model.

Espen Harlinn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 25 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100