Tony Vitabile
Tony Vitabile

Reputation: 8614

Issue with ComboBox and ObservableCollection<string>

Here's the Xaml for my combobox:

<ComboBox Grid.Column="4"
          Grid.Row="3"
          ItemsSource="{Binding Path=Users, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:AdvancedSettingEditor}}}"
          Margin="5"
          x:Name="UserPicker"
          SelectedValue="{Binding Path=StandAloneUserName, Mode=TwoWay}"
          TabIndex="1"
          Visibility="{Binding Converter={StaticResource InvertedBoolToVisibility}, Path=LoginRequired}" />

In the code behind, I have a simple ObservableCollection of strings:

public ObservableCollection<string> Users { get; set; }

The Users collection is loaded with string data from the database.

There is also a DependencyProperty that is bound to the window's DataContext that has a property called StandAloneUserName. The object stored in this property implements INotifyPropertyChanged.

When I run the program, the combo box has all of the names in the Users collection in the drop down list, but the box is blank. The control is not losing the value of the field, so it just doesn't know what to display.

How do I get my ComboBox to display the name that is in the StandAloneUserName property?

Tony

Upvotes: 1

Views: 552

Answers (1)

Rachel
Rachel

Reputation: 132588

Set SelectedItem instead of SelectedValue

<ComboBox SelectedItem="{Binding Path=StandAloneUserName, Mode=TwoWay}" ... />

I'm assuming it's evaluating as "doesn't exist" since you don't have SelectedValuePath set to anything.

Upvotes: 2

Related Questions