Reputation: 11
I would like to have a ComboBox with two options "M" and "F" and set the selection from my code with a string of one of the values. This seems so basic it's embarassing to even ask. However, I haven't seen a single example that doesn't include 50 lines of code with custom classes, etc, etc. To make things even easier, I want to prefill the two option in my XAML. Why is this simple task making me feel like a mental midget? This is what I have:
<ComboBox x:Name="cboGender" >
<ComboBoxItem Tag="M" Content="M"></ComboBoxItem>
<ComboBoxItem Tag="F" Content="F"></ComboBoxItem>
</ComboBox>
Code Behind:
cboGender.SelectedValue = "M";
Please help before I smash my computer and go back to ASP.NET development forever.
Upvotes: 1
Views: 1308
Reputation: 96870
To a first approximation, if you ever manipulate WPF UI elements in code you're doing it wrong. Sure, you can create a combo box with an "M" and an "F" and preset its selection:
<ComboBox>
<ComboBoxItem>M</ComboBoxItem>
<ComboBoxItem>F</ComboBoxItem>
<ComboBox.SelectedIndex>0</ComboBox.SelectedIndex>
</ComboBox>
But then what? What do you do with that selection? If your answer involves something like this:
if cboGender.SelectedValue == "M"
you're traveling down a road that will lead you to writing programs that are very hard to maintain.
If, on the other hand, you create a class to bind your UI to, like this:
public class MyViewModel
{
public MyViewModelClass()
{
Gender = "M";
}
public IEnumerable<string> Genders { get { return new string[] { "M", "F" } };
public string Gender { get; set; }
}
Then your XAML - assuming that somewhere along the line you've set the DataContext
to an instance of MyViewModel
- becomes:
<ComboBox ItemsSource="{Binding Genders}" SelectedValue="{Binding Gender}"/>
Any other logic that uses the Gender
property now looks at this class, not the UI. So you can change the UI without affecting your logic, and vice versa.
(Note that while view models typically implement INotifyPropertyChanged
, you only need to do this if you're going to be changing the Gender
in your code and want the new values to be reflected in the UI.)
Upvotes: 0
Reputation: 185300
Add the SelectedValuePath
and it works:
<ComboBox x:Name="cboGender" SelectedValuePath="Content" >
<ComboBoxItem Tag="M" Content="M"></ComboBoxItem>
<ComboBoxItem Tag="F" Content="F"></ComboBoxItem>
</ComboBox>
(SelectedValuePath="Tag"
would work, too, having the same data in two places seems quite redundant either way though)
As a side-note, similar to Colin's answer you can set the items like this:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<ComboBox x:Name="cboGender">
<sys:String>M</sys:String>
<sys:String>F</sys:String>
</ComboBox>
(Both SelectedItem
and SelectedValue
work in this case)
Upvotes: 1
Reputation: 70160
Try the following:
cboGender.ItemsSource = new string [] { "M", "F" };
cboGender.SelectedItem = "M";
You need to set the ItemsSource to the collection of items. The ComboBox will then generate the ComboBoxItems for you.
Upvotes: 2
Reputation: 81313
Try this - cboGender.SelectedIndex = 0;
before smashing your system.. :)
Upvotes: 0