tags:

views:

257

answers:

3

Why i cant set selecteditem property programatically?

Im calling it from another XAML Window, that hace certaing controls , one of them is a ComboBox i was trying this :

            string tm = (from ea in db.EXAMENXATENCIONs where ea.codigo == Convert.ToInt32(numeroinforme) select ea.turnomedico).FirstOrDefault();
            demo.cboTurnoMed.SelectedItem  = tm;
            demo.cboTurnoMed.Text =  tm;

C# 3.5

Thanks!

+1  A: 

I'm not certain what type of object your ComboBox has in it, but you might try setting the SelectedValue rather than the SelectedItem.

palehorse
+1  A: 

Is the item you're trying to set as selected well in the ComboBox data source?
The SelectedItem property looks for the value you provide in data source and then select it if found.

Tyalis
What's showing null? The ComboBox.SelectedItem?I mean, have you added the item in ComboBox.Items collection before trying to set it as SelectedItem?
Tyalis
thanks is working now!
Angel Escobedo
+1  A: 

You can set the SelectedItem. But the objects must MATCH. They can't just have the same data, they must actually be the same object.

What you're doing when you set the SelectItem property is saying, "You (the combobox) have a collection of objects, and I want this particular one in your list to be the selected one". You are not actually giving the combobox a new item, if that clears it up.

Chris Holmes