Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF .NET4
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 Smile | :)
 
Cheers!
Posted 25-Feb-11 2:56am
paleGegg01.8K
Rate this: bad
good
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:
 
myComboBox.DataContext=Classes;
 
It goes like this:
<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 :
 
<combobox name="Acombo" itemssource="{Binding}" displaymemberbinding="{Binding Path=Yourproperty}" />
  Permalink  
v2
Comments
Espen Harlinn at 25-Feb-11 8:50am
   
Good reply, my 5
Tarun.K.S at 25-Feb-11 8:53am
   
Thanks!
raistu at 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
good
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.
  Permalink  
Comments
SAKryukov at 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
raistu at 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
good
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.
 
Regards
Espen Harlinn
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 25 Feb 2011
Copyright © CodeProject, 1999-2014
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