I bet this is something very easy#Update:It is not#, but I didnt't found a solotion purly in XAML
.
I have two objects: A ObservableCollection
with "Tasks
" as ItemsSource
of the Datagrid
. And a ObservableCollection
of string
s which should be the values in the combobox
.
The name of the Task
is bound to to the first column. So far so good, that works.
Now I want to add combobox
in the second column with some parameters to choose.
The selection should be bound to the task
's ExecutingCore
property.
I think I messed up something with the DataGridComboBoxColumn
ItemsSource
. How would I do this binding here to get my values as dropdown shown?
<DataGrid Grid.Column="3" ItemsSource="{Binding Database.list_Tasks}"
HorizontalAlignment="Left" Margin="21,52,0,0" VerticalAlignment="Top"
Height="262" Width="205" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Task" Binding="{Binding Task_Spec_Name}"/>
<DataGridComboBoxColumn
Header="Executing core" Width="100"
ItemsSource="{Binding list_CoreID}"
SelectedItemBinding="{Binding ExecutingCore, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
list_CoreID
is NOT a property of Database.list_Tasks
. ViewModel
is the DataContext
.
ViewModel.Database.list_Tasks
ViewModel.list_CoreID
Thats how it looks like.
Update
I tried the solution from EagleBeak:
<DataGridComboBoxColumn
Header="Executing core" Width="100"
ItemsSource="{Binding ElementName=TasksGrid, Path=DataContext.list_CoreID}"
SelectedItemBinding="{Binding ExecutingCore, Mode=TwoWay}"/>
</DataGrid.Columns>
But this leads to the error:
error:System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.list_CoreID; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=20169503); target property is 'ItemsSource' (type 'IEnumerable')
I found something in a blog: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
"WPF doesn’t know which FrameworkElement to use to get the DataContext, because the column doesn’t belong to the visual or logical tree of the DataGrid."
Is there a better solution?