0

i want to display a ComboBox on my DataGrid. But the ComboBox does not load the ObservableCollection. My ObservableCollection 'Projects' is on my ViewModel defined. The problem is not the DataContext. But when I define the ComboBox outside of my DataGrid, the binding works. Does anyone have an idea where my problem is?

ViewModel:

public Project SelectedProject
{
    get { return _project; }
    set
    {
        if (_project != value)
        {
            _project = value;
            OnPropertyChanged();
            _actions = _database.LoadActions(SelectedProject.Id);
            OnPropertyChanged(() => Actions);
        }
    }
}

public ObservableCollection<Project> Projects
{
    get { return _database.LoadProjects(); }
}

XAML:

<DataGridComboBoxColumn Header="Projekt:" Width="140" DisplayMemberPath="Name">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Projects, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Setter Property="SelectedItem" Value="{Binding SelectedProject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Projects, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
            <Setter Property="SelectedItem" Value="{Binding SelectedProject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
3
  • Note that setting UpdateSourceTrigger=PropertyChanged on a OneWay binding is pointless. It has no effect, because it controls how a TwoWay or OneWayToSource Binding updates the source property. Besides that, setting Mode=OneWay on the ItemsSource Binding and Mode=TwoWay on the SelectedItem Binding is redundant. These values are the default already. Commented Apr 21, 2017 at 8:51
  • The problem is the datacontext. The DataContext of the DataGrid is the ViewModel, but the context of the column is the object for that row. That's why the datagrid works, you define the columns and each item in the itemsSource becomes a row with that context, then the column can bind to properties of that item. Commented Apr 21, 2017 at 8:58
  • @Clemens Yes, you are right. I have just changed it. Commented Apr 21, 2017 at 9:27

1 Answer 1

1

Try this:

<Setter Property="ItemsSource" Value="{Binding DataContext.Projects, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>

The DataContext of the ComboBox in a DataGridComboBoxColumn is the corresponding object in the DataGrid's ItemsSource.

If your Projects collection is defined in the view model, you need to specify a RelativeSource for the binding to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That's the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.