2011-02-07 66 views
0

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

我敲我的头now.Please救救我的表吧!

+0

所以,你得到的MessageBox,但组合没有信息?我看到你正在设置combobBox2的DataContext,但你没有任何限制。 – Thomas 2011-02-07 20:09:47

回答

0

您未设置组合框2的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

+0

非常感谢您的回复。当你说设置itemsource为'正确的表'时,你的意思是什么,我设置了哪个表,你的意思是'ObjDs'DataSet? – MangoTable 2011-02-07 20:24:24