无法使级联组合框工作

时间:2011-02-07 19:54:47

标签: wpf data-binding combobox cascadingdropdown

这是我创建级联组合框的代码。我试图根据为段名称(combox1)选择的值填充Family Combobox(ComboBox2)。我能够使用静态资源填充第一个Combo,但我的第二个组合显示为空白。 我在第一个组合框的选择更改事件上调用了一个名为“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";
        }
    }    

我正在敲桌子。请救救我!

1 个答案:

答案 0 :(得分:0)

您没有设置comboBox2的ItemsSourceDataContext将简单地影响其上的所有绑定语句。如果您将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时,将文本插入Items属性的行将会失败,因为您无法同时使用ItemsSourceItems在一起。