wpf 问题绑定 DataGridComboBoxColumn.ItemsSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1724180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem binding DataGridComboBoxColumn.ItemsSource
提问by Shimmy Weitzhandler
I have 3 tables: Item - which is the DataContext - it has a navigation column Group Group - has a navigation column Category.
我有 3 个表:Item - 这是 DataContext - 它有一个导航列 Group Group - 有一个导航列 Category。
I want to have in the DataGrid both (Category & Group) columns and when I choose a category it should display in the group col only the Category.Groups.
我想在 DataGrid 中同时拥有(类别和组)列,当我选择一个类别时,它应该只在组列中显示 Category.Groups。
Here is the code I am working on:
这是我正在处理的代码:
<tk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}">
<tk:DataGrid.Columns>
<!--Works-->
<tk:DataGridComboBoxColumn
Header="Categroy"
DisplayMemberPath="Title"
SelectedValuePath="CategoryId"
SelectedValueBinding="{Binding Group.Category.CategoryId}"
ItemsSource="{Binding Context.Categories,
Source={x:Static Application.Current}}"
/>
<!--Look at these two things:-->
<!--This does work-->
<tk:DataGridTemplateColumn>
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl
ItemsSource="{Binding Group.Category.Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type data:Group}">
<TextBlock Text="{Binding Title}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
<!--But this does NOT work, even it's the same source-->
<!--Notice I even tried a dummy converter and doesnt reach there-->
<tk:DataGridComboBoxColumn
Header="Group"
DisplayMemberPath="Title"
SelectedValuePath="GroupId"
ItemsSource="{Binding Group.Category.Groups,
Converter={StaticResource DummyConverter}}"
SelectedValueBinding="{Binding Group.GroupId}"
/>
</tk:DataGrid.Columns>
</tk:DataGrid>
Update
Would you say the problem is that the ItemsSource property cannot be set to a non-static Binding?
I suspect so because even I set the ItemsSource to {Binding}
with the DummyConverter
it doesn't stop in the converter; and in the Category ComboBox it works fine.
更新
您会说问题是 ItemsSource 属性不能设置为非静态绑定吗?我怀疑是因为即使我将 ItemsSource 设置{Binding}
为DummyConverter
它也不会在转换器中停止;在 Category ComboBox 中它工作正常。
回答by Aran Mulholland
The columns in the datagrid don't have a datacontext, as they are never added to the visual tree. sound a bit weird but have a look at vince's blog, its got a good example of the visual layout. once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources..)
数据网格中的列没有数据上下文,因为它们永远不会添加到可视化树中。听起来有点奇怪,但看看文斯的博客,它有一个很好的视觉布局示例。绘制网格后,单元格具有数据上下文,您可以使用普通绑定(不是静态资源..)在其中设置组合框项目源
You can access the combo box items source as such:
您可以像这样访问组合框项目源:
<dg:DataGridComboBoxColumn>
<dg:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
</Style>
</dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
Have a look hereand also herefor some code. You will also need to set the items source for the non edting elementas in this post
回答by John Henckel
I was using MVVM and I wanted to bind the ItemSource
of the column to a collection of objects in the window data context. I must have tried 10 different ways and nothing workeduntil I found this answer.
我正在使用 MVVM,我想将ItemSource
列的绑定到窗口数据上下文中的对象集合。在找到这个答案之前,我一定尝试了 10 种不同的方法,但没有任何效果。
The trick is to define a CollectionViewSource
outside the grid and then reference it inside the grid using StaticResource
. For example,
诀窍是CollectionViewSource
在网格外部定义 a ,然后使用StaticResource
. 例如,
<Window.Resources>
<CollectionViewSource x:Key="ItemsCVS" Source="{Binding MyItems}" />
</Window.Resources>
<!-- ... -->
<DataGrid ItemsSource="{Binding MyRecords}">
<DataGridComboBoxColumn Header="Column With Predefined Values"
ItemsSource="{Binding Source={StaticResource ItemsCVS}}"
SelectedValueBinding="{Binding MyItemId}"
SelectedValuePath="Id"
DisplayMemberPath="StatusCode" />
</DataGrid>