0

カスケード コンボボックスを作成するコードは次のとおりです。セグメント名 (combox1) で選択した値に基づいて、ファミリ コンボボックス (ComboBox2) を設定しようとしています。最初のコンボ ボックスには静的リソースを設定できますが、2 番目のコンボ ボックスには空白が表示されます。最初のコンボボックスの選択変更イベントで「FillComboBoxFamilyData(SegmentCode)」というメソッドを呼び出しています。

XAML コード:

<Grid Height="142" HorizontalAlignment="Left" Margin="49,113,0,0" Name="grid3"     VerticalAlignment="Top" Width="904">
                <ComboBox Height="23" HorizontalAlignment="Left" Margin="35,26,0,0"    Name="comboBox1" VerticalAlignment="Top" Width="205" ItemsSource="{Binding Source={StaticResource tblSegmentViewSource}}"  DisplayMemberPath="Segment Name" SelectedValuePath="Segment Code" SelectionChanged="comboBox1_SelectionChanged"/>
                <ComboBox Height="23" HorizontalAlignment="Right" Margin="0,26,395,0"    Name="comboBox2" VerticalAlignment="Top" Width="205" />


  <Window.Resources>
  <CollectionViewSource x:Key="tblSegmentViewSource" Source="{Binding Path=TblSegment,    Source={StaticResource brickDataset}}" />
    <CollectionViewSource x:Key="tblFamilyViewSource" Source="{Binding Path=TblFamily,  Source={StaticResource brickDataset}}" />

* BrickDataSet は、テーブルをプルしているメインのデータセットです。*

C#:

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedValue.ToString());
        SegmentCode = Convert.ToInt32(comboBox1.SelectedValue.ToString());
        FillComboBoxFamilyData(SegmentCode);
    }

    public void FillComboBoxFamilyData(int Segment_Code)
    {
        string connString =
            "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\dchaman\\My Documents\\PDRT.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True ";

        SqlConnection con = new SqlConnection(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText =
            "SELECT  TblFamily.[Family Name],TblFamily.[Family Code] FROM TblFamily WHERE (TblFamily.[Segment Code] = @SegmentCode)";

        cmd.Parameters.AddWithValue("@SegmentCode", Segment_Code);

        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(objDs);
        con.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            MessageBox.Show("Here I am");
            comboBox2.DataContext = objDs.Tables;
            comboBox2.Items.Insert(0, "--Select Family Name--");
            comboBox2.DisplayMemberPath = "Family Name";
            comboBox2.SelectedValue = "Family Code";
        }
    }    

私は今、テーブルで頭をぶつけています。私を助けてください!

4

1 に答える 1

0

あなたはcomboBox2のを設定していませんItemsSource。will は、DataContextすべてのバインドステートメントに影響を与えるだけです。ItemsSourceの代わりに を正しいテーブルに設定すると、正しいDataContextパスに移動するはずです。

if (objDs.Tables[0].Rows.Count > 0)
{
    MessageBox.Show("Here I am");
    comboBox2.ItemsSource = ((IListSource)objDs.Tables[0]).GetList(); // set the ItemsSource instead of the DataContext
    comboBox2.DisplayMemberPath = "Family Name";
    comboBox2.SelectedValue = "Family Code";
}

を設定すると、 と を同時に使用できないため、プロパティにItemsSourceテキストを挿入する行が失敗することに注意してください。ItemsItemsSourceItems

于 2011-02-07T20:11:17.793 に答える