简体   繁体   中英

Can't bind selection to WPF Combobox

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.

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.

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 )

Try this - cboGender.SelectedIndex = 0; before smashing your system.. :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM