开发者

Binding DataGridComboBoxColumn ItemsSource to RelativeSource FindAncestor doesn't work

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"


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜