问题结合DataGridComboBoxColumn.ItemsSource [英] Problem binding DataGridComboBoxColumn.ItemsSource

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

问题描述

我有3个表:
物品 - 这是DataContext的 - 它有一个导航栏组
集团 - 拥有一个导航栏分类

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

我想在DataGrid中有两个(类别及放大器;集团)列,当我选择一个类别,它应该在集团COL只显示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.

下面是code我的工作:

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属性不能被设置为一个非静态绑定?
我怀疑是因为连我自己设置的ItemsSource为 {结合} DummyConverter 它不会在变频器停止;而在类别组合框正常工作。

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.

推荐答案

在DataGrid中列没有一个DataContext,因为他们永远不会加入到可视化树。听起来有点不可思议,但看看<一个href=\"http://blogs.msdn.com/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx\"相对=nofollow>文斯的博客,它得到了可视化布局的一个很好的例子。一旦绘制网格单元有一个数据环境,您可以在其中使用普通的绑定设置组合框项目源(不是静态的资源。)

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>

看一看<一个href=\"http://stackoverflow.com/questions/1633800/wpf-datagrid-datagridcomboxbox-itemssource-binding-to-a-collection-of-collection\">here也这里一些code。您还需要为设置的项目源非edting元素在此

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天全站免登陆