Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF中的主细节组合_C#_Wpf_Data Binding_Master Detail_Master Slave - Fatal编程技术网

C# WPF中的主细节组合

C# WPF中的主细节组合,c#,wpf,data-binding,master-detail,master-slave,C#,Wpf,Data Binding,Master Detail,Master Slave,我是WPF的新手,特别是它的数据绑定 我有一个递归结构的表。表结构如下图所示: ID (int, not null) As PK, Text (nvarchar), ParentID (int) 我在表单上有两个组合,第一个组合由以下查询返回的项目填充: Select * from tbl where ParentID = null 选择此组合中的项目会导致加载表中ParentID等于所选项目ID的每一行。以下查询返回填充第二个组合的项目: Select * from tbl where

我是WPF的新手,特别是它的数据绑定

我有一个递归结构的表。表结构如下图所示:

ID (int, not null) As PK,
Text (nvarchar),
ParentID (int) 
我在表单上有两个组合,第一个组合由以下查询返回的项目填充:

Select * from tbl where ParentID = null
选择此组合中的项目会导致加载表中ParentID等于所选项目ID的每一行。以下查询返回填充第二个组合的项目:

Select * from tbl where ParentID = Selected_ID_form_Combo1
我对这个问题的解决方案(如果它能成为解决方案的话!):

我通过以下查询将数据加载到数据集中,但加载到两个不同的数据表中:

SqlConnection cnn = new SqlConnection(ConnectionString);

DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("Select * from tbl where ParentID is NULL", cnn);
SqlCommand cmd1 = new SqlCommand("Select * from tbl where ParentID != 0", cnn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

da.Fill(ds, "tbl");
da1.Fill(ds, "Child_tbl");
然后我创建一个视图,将子表的TargetID与主表的ID关联起来:

ds.Relations.Add(
"Rel_ID_ParentID",
ds.Tables["tbl"].Columns["ID"],
ds.Tables["Child_tbl"].Columns["ParentID"]);
最后,我将此istance的DataContext设置为数据源

this.DataContext=ds

XAML代码中的两个组合定义如下:

<ComboBox Name="cmbMain" ItemsSource="{Binding}"
   DataContext="{Binding Path=Tables[tbl]}" DisplayMemberPath="Text"
   SelectedValuePath="ID" />

<ComboBox Name="cmbChild" ItemsSource="{Binding Path=Rel_ID_ParentID}"
   DisplayMemberPath="Text" SelectedValue="ID" SelectedValuePath="TargetID"
   IsSynchronizedWithCurrentItem="True"/>


但是第二个组合框没有填充任何数据。有什么想法吗?

“不起作用”在堆栈溢出时不起作用。你到底想发生什么不起作用,有例外吗?第二个组合根本没有填充任何数据!它显示一个空列表。您似乎没有使用Combo1的
SelectionChanged
IsSynchronizedWithCurrentItem
用于将组合框与相同的ItemsSource同步。请提供一个简单的示例。只有两个组合在WPF中互相更新。谢谢