2012-06-08 106 views
1

我想要做類似於Data bound WPF ComboBox with choices defined in XAML?的操作,但我想在數據表中指定SettingValues中的選項。該數據表將包含三列:SettingType(int),SettingId(int)和Description。我想在組合框中顯示特定SettingType(例如1)的所有行的描述。這個設置類型我想爲每個設置進行硬編碼。然後,我選擇的SettingId將保存在具有列Setting1的另一個表格中。在數據表中綁定帶有選項的WPF組合框

這個問題似乎也做了類似的事情:how to bind ComboBox with DataTable

第一次嘗試(代碼片段):

<ComboBox SelectedValue="{Binding Setting1}" SelectedValuePath="SettingId"> 
<ComboBox.ItemTemplate> 
    <DataTemplate> 
    <TextBlock Text="{Binding Path=Description}"/>         
    </DataTemplate> 
</ComboBox> 

如何指定要使用的表?現在只有指定的列。如何指定只使用特定的行,其SettingType匹配某個int?

+0

第一個問題解決了,第2部分在http://stackoverflow.com/questions/10976358/using-wpf-combobox-to-select-item-from-one-datatable-and-save-to-another-datatab – flafffl

回答

1

通過使用

var dataRows1 = svds.SettingValues.Select("SettingType = 1"); 
cbSetting1.ItemsSource = dataRows1.AsEnumerable(); 

通過使用

<ComboBox x:Name="cbSetting1" ItemsSource="{Binding}"> 
<ComboBox.ItemTemplate> 
    <DataTemplate> 
    <TextBlock Text="{Binding Path=ValueDescription}" /> 
    </DataTemplate> 
</ComboBox.ItemTemplate> 
</ComboBox> 

在的.cs文件的DataContext設置與SettingType == 1行解決的從數據表中讀取的值的問題其中svds是一個DataSet。

在會話之間保存選定的設置是通過使用cbSetting1.SelectedItem來設置和獲取值。