12

我有 3 个表: Item - 这是 DataContext - 它有一个导航列 Group Group - 有一个导航列 Category。

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

这是我正在处理的代码:

<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 设置为{Binding}DummyConverter也不会在转换器中停止;在类别组合框中它工作正常。

4

2 回答 2

32

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

您可以像这样访问组合框项目源:

<dg:DataGridComboBoxColumn>
   <dg:DataGridComboBoxColumn.EditingElementStyle>
      <Style TargetType="ComboBox">
         <Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
      </Style>
   </dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>

看看这里这里的一些代码。您还需要为非编辑元素设置项目来源,本文所述

于 2009-11-15T11:42:55.280 回答
7

我正在使用 MVVM,我想将ItemSource列的绑定到窗口数据上下文中的对象集合。在找到这个答案之前,我一定尝试了 10 种不同的方法,但没有任何效果

诀窍是CollectionViewSource在网格外部定义一个,然后在网格内部使用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>
于 2017-08-29T19:01:31.283 回答