DataGridComboBoxColumn的ItemSource绑定不工作 [英] DataGridComboBoxColumn ItemSource Binding Doesn't work

查看:287
本文介绍了DataGridComboBoxColumn的ItemSource绑定不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有的DataGridComboBoxColumn的ItemSource从资源正在添加的例子。
难道不是直接绑定到一个列表中的codeBehind?

In all of the examples of DataGridComboBoxColumn ItemSource comming from the Resources. Couldn't it be binding directly to a list in the CodeBehind ?

推荐答案

这取决于你的意思在codeBehind直接绑定到一个列表的东西。

It depends what you mean with binding directly to a list in the CodeBehind.

您可以用声明...列

<DataGridComboBoxColumn x:Name="m_column" ../>

,然后在code-背后设置的ItemsSource ...

and then in code-behind set the ItemsSource...

m_column.ItemsSource=yourItemsSource

然而您不能直接使用XAML ,类似的绑定:

<DataGridComboBoxColumn ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=YourElement}}"/>

由于 DataGridComboBoxColumn 不是可视化树的一部分。

because DataGridComboBoxColumn is not a part of the visual tree.

有这个解决方法。最简单的是使用DataGridTemplateColumn并直接在编辑-DataTemplate中放置组合框。如果您使用视图模型,你可以通过它提供的数据。否则,看<一个href=\"http://joemorrison.org/blog/2009/02/17/excedrin-headache-35401281-using-combo-boxes-with-the-wpf-datagrid/\">here和<一个href=\"http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx\">here对于解决办法。

There are workarounds for this. The most simple is using a DataGridTemplateColumn and placing the ComboBoxes directly in the edit-DataTemplate. If you use a ViewModel, you can provide the data through it. Otherwise look here and here for workarounds.

这篇关于DataGridComboBoxColumn的ItemSource绑定不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆