2011-02-07 68 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