I am writing what is turning out to be a simple GUI in WPF. At the moment I have a static list inside of a ComboBox, like this:
<ComboBox HorizontalAlignment="Left" Height="22" Margin="24,97,0,0" VerticalAlignment="Top" Width="83"
SelectedItem="{Binding fruit, Mode=TwoWay}">
<ComboBoxItem>apple</ComboBoxItem>
<ComboBoxItem>orange</ComboBoxItem>
<ComboBoxItem>grape</ComboBoxItem>
<ComboBoxItem>banana</ComboBoxItem>
</ComboBox>
I’m binding the SelectedItem to a singleton in my code that has already been initialized and used elsewhere.
I put a breakpoint on the get of fruit and it returns “grape”, but the selected item is always blank. I even added a button so that I could call RaisePropertyChanged manually, but the RaisePropertyChange call didn’t do anything either.
Finally, MVVMLight gives blendability. For no important reason I changed the binding in the combobox from SelectedItem to Text As soon as I did that, my design time form filled in with the expected values, but, when the code is running, the box continues to sit at the empty state
This is because you have items of type
ComboBoxItemin theComboBoxbut the property you are trying to bind to is of typestring.You have three options:
1.Instead of adding
ComboBoxItemitems addStringitems:2.Instead of
SelectedItembind toSelectedValueand specifySelectedValuePathasContent:3.Do not specify items directly in XAML, but use
ItemsSourceproperty to bind to a collection of strings: