将 DataGridComboBoxColumn ItemsSource 绑定到relativeSource FindAncestor 不起作用

发布于 2024-09-08 14:16:35 字数 1870 浏览 1 评论 0原文

我正在尝试使用 WPFToolkit 的 DataGrid 控件(和 C#/.Net 3.5)来显示每个记录的 ComboBox。使用以下代码,组合框会显示,但其下拉列表不包含任何项目:

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

此外,Visual Studio 的输出窗口显示以下错误:

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')

但是,以下代码按预期工作(组合框的下拉列表已正确填充):

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

注意DataGrid 和 ItemsControl 具有相同的 ItemsSource 字符串。 DataGridComboBoxColumn 和 ComboBox 也是如此。一个控件绑定正确,而另一个控件绑定不正确。

为什么 DataGridComboBoxColumn ItemsSource 无法正确绑定?

谢谢你,
Ben

仅供参考,diagnostics 定义为 xmlns:diagnostics="clr-namespace:System.Diagnostics; assembly=WindowsBase"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2024-09-15 14:16:35

有趣的是...如果我创建一个包含 ComboBox 的自定义 DataGridColumn 并使用与上面给出的相同的 ItemsSource 绑定字符串,它就可以工作。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文