不能使级联组合框工作 [英] Not able to make cascading comboboxes work

查看:171
本文介绍了不能使级联组合框工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码创建级联组合框。我试图根据为段名称(combox1)选择的值填充Family Combobox(ComboBox2)。我能够使用静态资源填充第一个Combo,但第二个显示为空。
我在第一个组合框的选择更改事件上调用一个名为FillComboBoxFamilyData(SegmentCode)的方法。

Here is my code to create cascading comboboxes. I am trying to populate Family Combobox(ComboBox2) based on the value selected for Segment Name(combox1).I am able to populate the first Combo with a static resource but my second one shows blank. I am calling a method called "FillComboBoxFamilyData(SegmentCode)" on the selection change event of first combobox.

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是我正在拉取表格的主要数据集。 *

*BrickDataSet is the main dataset I am pulling the tables from.*

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";
        }
    }    

我用桌子撞我的头现在请保存我!

推荐答案

您没有设置comboBox2的 ItemsSource DataContext 将简单地影响所有绑定语句。如果您将 ItemsSource 设置为正确的表,而不是 DataContext ,那么应该让您处于正确的路径: / p>

You aren't setting the comboBox2's ItemsSource. The DataContext will simply effect all binding statements on it. If you set the ItemsSource to the correct table instead of the DataContext, it should get you on the right path:

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 属性的行将失败,因为您不能同时使用 ItemsSource 项目在一起。

Note that when you set the ItemsSource, the line that inserts text into the Items property will fail, as you can not use both the ItemsSource and the Items together.

这篇关于不能使级联组合框工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆