0
votes

EDIT
Solution was actually a proper setting of SelectedItem, SelectedValue and SelectedValuePath properties.

<ComboBox
         Grid.Column="1"
         Padding="5"
         DisplayMemberPath="PositionName"
         IsSynchronizedWithCurrentItem="False"
         ItemsSource="{Binding Positions, Mode=OneWay}"
         SelectedItem="{Binding SelectedOperatorPosition, Mode=TwoWay, 
         UpdateSourceTrigger=PropertyChanged}"
         SelectedValue="{Binding SelectedOperatorPosition.PositionName}"
         SelectedValuePath="PositionName" />

QUESTION I am trying to bind ComboBox SelectedValue with DataGrid SelectedItem using MVVM pattern.
ComboBox ItemSource is Shifts property. I want it's SelectedValue to be bound with SelectedShift property, which is updated each time user selects another OperatorModel from DataGrid.
Although SelectedOperator setter sets SelectedShift value to SelectedOperator.Shift, ComboBox doesn't get updated.

View
Debugging ViewModel Property setter

My ViewModel:

private Operator selectedOperator;

public Operator SelectedOperator
{
    get
    {
        return selectedOperator;
    }
    set
    {
        selectedOperator = value;
        if (selectedOperator != null)
        {
            SelectedShift = selectedOperator.Shift;
        }
        OnPropertyChanged(nameof(SelectedOperator));
    }
}

private Shift selectedShift;

public Shift SelectedShift
{
    get
    {
        return selectedShift;
    }
    set
    {
        selectedShift = value;
        OnPropertyChanged(nameof(SelectedShift));
    }
}

XAML:

<ComboBox
         Grid.Column="1"
         Padding="5"
         DisplayMemberPath="Name"
         ItemsSource="{Binding Shifts}"
         SelectedValue="{Binding SelectedShift}"
         SelectedValuePath="Name" />
1

1 Answers

0
votes

i think you should use SelectedItem="{Binding SelectedShift}