2011-06-21 184 views
1

我想在組合框wpf中獲取選定的項目。但它返回System.Data.DataRowView。我的代碼在XAML:組合框選定的項目WPF c#

<ComboBox Name="ddDeputi" Margin="131,85,0,0" Width="327" HorizontalAlignment="Left" VerticalAlignment="Top" Height="22" 
     SelectedValue="{Binding Path = kodeDep, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" 
       Validation.ErrorTemplate="{x:Null}" SelectionChanged="ddDeputi_SelectionChanged" Loaded="ddDeputi_Loaded" /> 

而且在我的.cs:

private void ddDeputi_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

     if (ddDeputi.SelectedIndex != -1) 
     { 
      string akode = ddDeputi.SelectedValue.ToString(); 
      DaUnitKerja oDa = new DaUnitKerja(); 
      DataSet data = new DataSet(); 
      data = oDa.TampilDir(akode); 
      ddDir.ItemsSource = data.Tables[0].DefaultView; 
      ddDir.DisplayMemberPath = data.Tables[0].Columns["unit_kerja"].ToString(); 
      ddDir.SelectedValuePath = data.Tables[0].Columns["kode"].ToString(); 
      ddSubDir.SelectedIndex = -1; 
      ComboBoxItem cbi = (ComboBoxItem)ddDeputi.ItemContainerGenerator.ContainerFromItem(ddDeputi.SelectedItem); 
      txtDeputi.Text = cbi.Content.ToString(); 



     } 
    } 

回答

2

當您綁定到ADO.NET您總是數據綁定到DataView。每個DataRowView的包裝在源數據表一個DataRow

您可以通過獲取選中的DataRow如下:

DataRowView selectedDataRowView = (DataRowView)ddDir.SelectedItem; 
DataRow selectedRow = selectedDataRowView.Row; 
0

當你定義的選擇更改事件ü可以很容易地從表中得到任何值綁定到ddDeputi由索引值.... 使用comboBox1.SelectedIndex在事件ddDeputi_SelectionChanged表索引..

像這樣

String value = data.Tables[0].Rows[ddDeputi.SelectedIndex]["kode"].ToString(); 

和其他方式的投你的組合框中選擇項comboboxitem,並用它

System.Windows.Controls.ComboBoxItem currentItem=((System.Windows.Controls.ComboBoxItem)ddDeputi.SelectedItem); 

    string myvalue=currentItem.Content; 

這裏myvalue的插上U選定的值。

相關問題