Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox SelectedItem binding not updating

I'm a bit puzzled: this works:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Rol" />
            <ComboBox ItemTemplate="{StaticResource listRollen}"
                      Height="23" Width="150"
                      SelectedItem="{Binding Path=SelectedRol, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      ItemsSource="{Binding Path=allRollen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>

and the property for SelectedRol is:

public TblRollen SelectedRol
    {
        get { return _selectedRol; }
        set
        {
            if (_selectedRol != value)
            {
                _selectedRol = value;
                OnPropertyChanged("SelectedRol");
            }
        }
    }

But this doesn't work:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Soort" />
            <ComboBox ItemTemplate="{StaticResource listSoorten}"
                      Height="23" Width="150"
                      ItemsSource="{Binding Path=allSoorten}"
                      SelectedItem="{Binding Path=SelectedProduct, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>

with following property SelectedProduct:

public TblProduktSoorten SelectedProduct
    {
        get { return _selectedPSoort; }
        set
        {
            if (_selectedPSoort != value)
            {
                _selectedPSoort = value;
                OnPropertyChanged("SelectedProduct");
            }
        }
    }

somewhere in my code I set SelectedProduct = p.TblProduktSoorten and while debugging, I see the property gets set correctly...

like image 262
olvr_vrmr Avatar asked Jun 03 '11 11:06

olvr_vrmr


2 Answers

Combobox inside a DataGrid?

If the combobox is in a DataGrid you must add this :

Mode=TwoWay, UpdateSourceTrigger=PropertyChanged

See this : https://stackoverflow.com/a/5669426/16940

like image 123
Simon_Weaver Avatar answered Sep 19 '22 06:09

Simon_Weaver


This might be related to the fact that apparently attribute order does matter, in your second case the ItemsSource and SelectedItem declarations are swapped.

like image 42
H.B. Avatar answered Sep 21 '22 06:09

H.B.