Reputation: 47
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>
Upvotes: 0
Views: 186
Reputation: 169200
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.
Upvotes: 1