0

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?

Community
  • 1
  • 1
Andreas
  • 25
  • 6
  • Sorry man. My solution didn't work and all of a sudden I'm under siege here at work. I'm afraid I have to back out from the discussion. But maybe this helps: http://stackoverflow.com/questions/2890156/how-to-bind-collection-to-wpfdatagridcomboboxcolumn – EagleBeak Feb 05 '15 at 10:31
  • No problem, thanks anyway. I did it now in code behind, this is working without problems. – Andreas Feb 05 '15 at 10:35
  • Hey man. Just checked back on your issue and looked at the link from Levesque's blog you posted. Now I remember that someone in a team I worked in had this problem a few years ago. And the BindingProxy solution is exactly what we did back then. Much better than code behind IMO. – EagleBeak Feb 05 '15 at 16:42

1 Answers1

0

I have also came cross this kind of problem. Following way I have work around it, in XAML

<wpf:DataGridComboBoxColumn 
 x:Name="cboExecutingcore" 
 Header="Executing core" 
 SelectedItemBinding="{Binding Path =Executingcore, Mode=TwoWay}"/>

Then I have bind the cboExecutingcore from code behide.

cboExecutingcore.ItemsSource = list_CoreID;
cboExecutingcore.DisplayMemberPath = "Display Property"
Nazmul
  • 575
  • 3
  • 18
  • Exactly thats what I did and as far as I know the only way without fiddling around... – Andreas Feb 05 '15 at 15:36
  • I have spend more than one day in one of my case. But did not able to bind it from XAML. Data has saved correctly but combo box does not show any value. – Nazmul Feb 08 '15 at 15:21