Master
Master

Reputation: 2163

Binding to a combobox with specified content

I'm wondering how can I bind to a ComboBox item when it's content is specified. I'm able to save the content to the database but when I try to re-enter the record, the ComboBox won't change at all to the SelectedItem.

<ComboBox SelectedItem="{Binding Duration, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0" Style="{StaticResource CombBox}" MinWidth="60">
    <ComboBoxItem Content="15 Minutes"/>
    <ComboBoxItem Content="30 Minutes"/>
    <ComboBoxItem Content="45 Minutes"/>
    <ComboBoxItem Content="1 Hour"/>
</ComboBox>

public string Duration { get { return Entity.Duration; } set { Entity.Duration = value; NotifyOfPropertyChange(); } }

Am I not setting my bindings correct?

Upvotes: 2

Views: 60

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32511

First: In you xaml use this namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"

Second: change your ComboBox to this:

<ComboBox SelectedItem="{Binding Duration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <sys:String>15 Minutes</sys:String>
    <sys:String>30 Minutes</sys:String>
    <sys:String>45 Minutes</sys:String>
</ComboBox>

Third: How did you implement the INotifyPropertyChanged? It seems you don't send the properties name as string to the Notify mechanism. It should be something like this (insted of NotifyOfPropertyChange())

PropertyChanged(this, new PropertyChangedEventArgs("Duration"));

Upvotes: 1

Arnel
Arnel

Reputation: 332

Try to specify the binding mode, Mode = TwoWay

For more info go to https://msdn.microsoft.com/en-us/library/system.windows.data.binding.mode(v=vs.110).aspx

Hope this helps

Upvotes: 1

Related Questions