问题绑定 DataGridComboBoxColumn.ItemsSource [英] Problem binding DataGridComboBoxColumn.ItemsSource

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

问题描述

我有 3 张桌子:Item - 这是 DataContext - 它有一个导航列 Group组 - 有一个导航栏类别.

I have 3 tables: Item - which is the DataContext - it has a navigation column Group Group - has a navigation column Category.

我想在 DataGrid 中同时包含(类别和组)列,当我选择一个类别时,它应该只在组列中显示 Category.Groups.

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.

这是我正在处理的代码:

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>

更新
你会说问题是 ItemsSource 属性不能设置为非静态绑定吗?我怀疑是因为即使我使用 DummyConverter 将 ItemsSource 设置为 {Binding} 它也不会在转换器中停止;并且在 Category ComboBox 中它工作正常.

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.

推荐答案

数据网格中的列没有数据上下文,因为它们永远不会添加到可视化树中.听起来有点奇怪,但看看 vince 的博客,它有一个很好的视觉布局示例.绘制网格后,单元格具有数据上下文,您可以使用普通绑定(不是静态资源..)在其中设置组合框项目源.

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 here and also here for some code. You will also need to set the items source for the non edting element as in this post

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

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