Jonathan Escobedo
Jonathan Escobedo

Reputation: 4053

properties in ComboBox in WPF

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!

Upvotes: 0

Views: 595

Answers (3)

Michaël Carpentier
Michaël Carpentier

Reputation: 2135

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.

Upvotes: 1

Chris Holmes
Chris Holmes

Reputation: 11574

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.

Upvotes: 1

palehorse
palehorse

Reputation: 27486

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

Upvotes: 1

Related Questions