Click here to Skip to main content
15,885,885 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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)
{
myComboBox.Items.Add(c);
}


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 :)

Cheers!
Posted

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:

C#
myComboBox.DataContext=Classes;


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


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 :

XML
<combobox name="Acombo" itemssource="{Binding}" displaymemberbinding="{Binding Path=Yourproperty}" />
 
Share this answer
 
v2
Comments
Espen Harlinn 25-Feb-11 8:50am    
Good reply, my 5
Tarun.K.S 25-Feb-11 8:53am    
Thanks!
paleGegg0 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 :)
Are you sure they are the same reference? A data-copy of the original instance won't be considered the same.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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)
--SA
paleGegg0 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?
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.

Regards
Espen Harlinn
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900