带有2个DataGridComboBoxColumns的WPF DataGrid根据第一个组合框的选择更新第二个组合框

时间:2014-04-10 19:21:09

标签: c# wpf xaml datagrid combobox

我有一个绑定到datasoruce的Datagrid。此外,还有2个由静态列表填充的DataGridComboBoxColumn。 DataGridComboBoxColumn的绑定类似于:

<DataGridComboBoxColumn Header="Category" Width="150" SelectedValuePath="ID" SelectedValueBinding="{Binding Category, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="CategoryName" ItemsSource="{Binding ReturnCategories, Source={StaticResource Typeslist}}"/>

<DataGridComboBoxColumn Header="Sub Category" Width="150" SelectedValuePath="ID" SelectedValueBinding="{Binding SubCategory, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="CategoryName" ItemsSource="{Binding ReturnSubCategories, Source={StaticResource Typeslist}}"/>

加载数据网格时,主类别组合框将填充类别,并根据记录集正确选择。 第二个组合框也是填充的。我想要实现的是每当我更改主类别组合框时,子类别组合框需要根据主要类别的选择加载相应的值。 目前,主类和子类的静态资源不接受任何参数。是否可以将参数传递给子类别静态资源,以便加载相应的列表。

静态资源由XAML调用的数据库填充。

static List<MainCategories> mainCatergoryBuffer = new List<MainCategories>();
static List<SubCategories> subCatergoryBuffer = new List<SubCategories>();

如果我应该根据主要类别选择更改子类别内容,这是否意味着子类别的其他行值也会受到影响?

我该如何解决这个问题?

网格示例: enter image description here

1 个答案:

答案 0 :(得分:1)

修改

您不会轻松能够做到这一点,这些组合框不会继承数据网格数据上下文。

http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Windows.Controls.DataGridComboBoxColumn);k(VS.XamlEditor);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5)&rd=true

Binding in a WPF data grid text column

https://www.google.fr/search?q=Cannot+find+governing+FrameworkElement+or+FrameworkContentElement+for+target+element.&oq=Cannot+find+governing+FrameworkElement+or+FrameworkContentElement+for+target+element.&aqs=chrome..69i57j0l4.1185j0j7&sourceid=chrome&es_sm=122&ie=UTF-8

WPF Error: Cannot find governing FrameworkElement for target element

建议:使用Xceed的免费数据网格,您不会遇到此类问题

https://wpftoolkit.codeplex.com/


这是一个关于如何实现这一目标的非常简单的例子,

很明显,您想要适应当前的课程。

enter image description here enter image description here

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication5
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = new MyObject();
        }
    }

    internal class MyObject
    {
        public MyObject()
        {
            AvailableCategories = new ObservableCollection<Category>(new List<Category>(new[]
            {
                new Category
                {
                    Name = "category1",
                    SubCategories = new List<Category>(new[]
                    {
                        new Category {Name = "subCategory1a"},
                        new Category {Name = "subCategory1b"}
                    })
                },
                new Category
                {
                    Name = "category2",
                    SubCategories = new List<Category>(new[]
                    {
                        new Category {Name = "subCategory2a"},
                        new Category {Name = "subCategory2b"}
                    })
                }
            }));
        }

        public ObservableCollection<Category> AvailableCategories { get; private set; }
    }

    public class Category
    {
        public string Name { get; set; }

        public List<Category> SubCategories { get; set; }

        public override string ToString()
        {
            return String.Format("Name: {0}", Name);
        }
    }
}

和XAML:

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApplication5="clr-namespace:WpfApplication5"
        Title="MainWindow"
        Width="300"
        Height="300"
        Loaded="Window_Loaded"
        mc:Ignorable="d">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="DataTemplateCategory" DataType="wpfApplication5:Category">
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </Grid.Resources>
        <StackPanel>
            <ComboBox x:Name="ComboBox1"
                      ItemTemplate="{StaticResource DataTemplateCategory}"
                      ItemsSource="{Binding AvailableCategories}"
                      d:DataContext="{d:DesignData MyClass}" />
            <ComboBox DataContext="{Binding ElementName=ComboBox1,
                                            Path=SelectedItem}"
                      ItemTemplate="{StaticResource DataTemplateCategory}"
                      ItemsSource="{Binding Path=SubCategories}"
                      d:DataContext="{d:DesignData Category}" />
        </StackPanel>
    </Grid>
</Window>