0

私はWPFの初心者で、特にそのデータバインディングです。

再帰構造のテーブルがあります。テーブル構造は次のようになります。

ID (int, not null) As PK,
Text (nvarchar),
ParentID (int) 

フォームに 2 つのコンボがあります。最初のコンボには、次のクエリによって返されるアイテムが入力されます。

Select * from tbl where ParentID = null

このコンボで項目を選択すると、選択した項目 ID と等しい ParentID を持つテーブル内のすべての行が読み込まれます。次のクエリは、2 番目のコンボを満たすアイテムを返します。

Select * from tbl where ParentID = Selected_ID_form_Combo1

この問題に対する私の解決策(それが解決策になる場合):

次のクエリでデータをデータセットに読み込みますが、2 つの異なるデータ テーブルに読み込みます。

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"]);

最後に、このインスタンスの DataContext をデータ ソースに設定します。

this.DataContext = ds;

XAML コードでは、2 つのコンボが次のように定義されています。

<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"/>

しかし、2 番目のコンボボックスにはデータが入力されていません。何か案は?

4

0 に答える 0