【问题标题】:Accessing control between DataGridCells, dynamic cascading ComboBoxesDataGridCells之间的访问控制,动态级联ComboBoxes
【发布时间】:2011-03-13 07:37:09
【问题描述】:

我有一个 DataGrid,它的两列是 ComboBoxes(一个包含很少,但不是这个问题)。

我希望,当用户更改第一个 Combo 的值时,另一列中的 ComboBox 应绑定到其属性(此属性是一个集合)。假设第一个 ComboBox 是类别,我希望当用户更改其值时,另一个 CB 填充(第一个组合的选定类别)的值。供应商。

我该怎么做,我不使用MVVM,只是简单的WPF。 我不知道什么应该是正确的实现方式,我希望我开始它是正确的。

我认为,如果我可以从第一个的 SelectionChangeHandler 中获得另一个 ComboBox(位于不同的 DataGridCell 中),那将是最好的,因为这样我就可以在第一个的每次选择更改时重置其源。 请注意,我有能力访问当前(第一个)DataGridCell,我只是在寻找一种有效的方法来访问正确的 DataGridCell 兄弟,然后获取其子(第二个)组合。

另请注意,所选类别应因行而异,第二个 ComboBox 应取决于该行的类别。
我实际上尝试实现它,以便将 CollectionViewSource.Source 绑定到当前项目(即行的 DataContext),但它似乎不起作用。
我更喜欢通过第一个 ComboBox 的 SelectionChange 处的操作触发器或处理程序来设置第二个组合的 CollectionViewSource (VendorsCollection)。

该字段中的其他 ComboBox 似乎没有问题,因为它们都相互绑定,我可能会使用 CollectionViewSource.Filter,无论如何访问它们不是问题,因为它们是简单的兄弟姐妹,不像第一个是位于另一个 DataGridCell 深处的远亲。

这是我到目前为止所尝试的:

<DataGrid>
    <DataGrid.Resources>
        <CollectionViewSource x:Key="CategoriesCollection" Source="{Binding Context.CategoriesList, Source={x:Static Application.Current}, IsAsync=True}" />
    </DataGrid.Resources>

    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Category">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock DataContext="{Binding Category}" Text="{Binding Title}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <!--This is the first ComboBox-->
                    <ComboBox
                                IsSynchronizedWithCurrentItem="False"
                                ItemsSource="{Binding Source={StaticResource CategoriesCollection}}"
                                DisplayMemberPath="Title"
                                SelectionChanged="cbCategories_SelectionChanged"
                                SelectedItem="{Binding Category}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Style">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock DataContext="{Binding Finish.Style.Vendor}" Text="{Binding Contact.Title}"/>
                        <TextBlock DataContext="{Binding Finish.Style}" Text="{Binding Title}"/>
                        <TextBlock Text="{Binding Finish.Title}"/>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <StackPanel>
                        <StackPanel.Resources>
                            <!--I want, that when the user selects a value in the first ComboBox,
                    the VendorsCollection below should be populated with the selected Category.Vendors,
                    or alternatively current row's data item.Category.Vendors,
                    I just donno how to access current row from these resources.-->
                            <CollectionViewSource x:Key="VendorsCollection" Source="{Binding Vendors, Source={StaticResource CategoriesCollection}}" />
                            <CollectionViewSource x:Key="StylesCollection" Source="{Binding Styles, Source={StaticResource VendorsCollection}}" />
                            <CollectionViewSource x:Key="FinishesCollection" Source="{Binding Finishes, Source={StaticResource StylesCollection}}" />
                        </StackPanel.Resources>
                        <ComboBox                                                       
                                    IsSynchronizedWithCurrentItem="True"
                                    ItemsSource="{Binding Source={StaticResource VendorsCollection}}"
                                    SelectedItem="{Binding Finish.Style.Vendor}"
                                    DisplayMemberPath="Contact.Title"/>
                        <ComboBox
                                    IsSynchronizedWithCurrentItem="True"
                                    ItemsSource="{Binding Source={StaticResource StylesCollection}}"
                                    SelectedItem="{Binding Finish.Style}"
                                    DisplayMemberPath="Title"/>
                        <ComboBox
                                    IsSynchronizedWithCurrentItem="True"
                                    ItemsSource="{Binding Source={StaticResource FinishesCollection}}"
                                    SelectedItem="{Binding Finish}"
                                    DisplayMemberPath="Title"/>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>
</DataGrid>

【问题讨论】:

    标签: wpf datagrid combobox cascadingdropdown datagridcell


    【解决方案1】:

    我刚刚遇到了你的问题。你的问题解决了吗?我认为您的问题类似于我得到的this one。希望那里的解决方案也对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多