views:

32

answers:

1

Hello,

I'm trying to use WPFToolkit's DataGrid control (and C#/.Net 3.5) to display a ComboBox per record. With the below code, the ComboBoxes show up but their drop-downs contain no items:

<wpftkit:DataGrid ItemsSource="{Binding TransactionToEdit.SisterTransactions}"
          AutoGenerateColumns="False">
<wpftkit:DataGrid.Columns>
    <wpftkit:DataGridComboBoxColumn Header="Account" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel},  diagnostics:PresentationTraceSources.TraceLevel=High}, Path=DataContext.Accounts}" DisplayMemberPath="Name"/>
</wpftkit:DataGrid.Columns>
</wpftkit:DataGrid>

Additionally, Visual Studio's output window shows the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with 
  reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.StackPanel', AncestorLevel='1''. 
  BindingExpression:Path=DataContext.Accounts; DataItem=null; target element is 
  'DataGridComboBoxColumn' (HashCode=25733404); target property is 
  'ItemsSource' (type 'IEnumerable')

However, the following code works as expected (the ComboBoxes' drop down lists are correctly populated):

<ItemsControl ItemsSource="{Binding TransactionToEdit.SisterTransactions}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts, diagnostics:PresentationTraceSources.TraceLevel=High}" DisplayMemberPath="Name"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Note that both the DataGrid and the ItemsControl have identical ItemsSource strings. So do the DataGridComboBoxColumn and the ComboBox. One control binds correctly and the other does not.

Why doesn't the DataGridComboBoxColumn ItemsSource bind properly?

Thank you,
Ben

FYI, diagnostics is defined as xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"

+1  A: 

Interesting...if I create a custom DataGridColumn containing a ComboBox and use the same ItemsSource binding string as given above, it works.

<wpftkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <ComboBox SelectedItem="{Binding Account}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=DataContext.Accounts}"        DisplayMemberPath="Name" />
    </DataTemplate>
</wpftkit:DataGridTemplateColumn.CellTemplate>
Ben Gribaudo
I found that if I put the ComboBox in the CellTemplate, strange things could happen...such as the ComboBox would losing its selected value when the row switched into edit mode. A simple fix for this was to have CellTemplate display a TextBlock and then place the ComboBox in CellEditingTemplate.
Ben Gribaudo
It is a bug, it has been raised
No hay Problema