【发布时间】:2016-12-30 07:13:52
【问题描述】:
我正在尝试将特定项目设置为 DataGridComboBoxColumn 中的 selectedItem。然而,经过大量研究,我还没有找到适合我的正确答案。
我的场景:
我有一个以编程方式创建的DataGrid,它的ObservableCollection<> 为ItemsSource。作为最后一列,我想添加一个DataGridComboBoxColumn 给用户一个可供选择的选项。由于此类数据已经可以存储在数据库中,因此我需要从存储在数据库中的集合中“预设”值。
private void ManipulateColumns(DataGrid grid)
{
...
DataGridComboBoxColumn currencies = new DataGridComboBoxColumn();
//Here come the possible choices from the database
ObservableCollection<string> allCurrencies = new ObservableCollection<string>(Data.AllCurrencys);
currencies.ItemsSource = allCurrencies;
currencies.Header = "Currency";
currencies.CanUserReorder = false;
currencies.CanUserResize = false;
currencies.CanUserSort = false;
grid.Columns.Add(currencies);
currencies.MinWidth = 100;
//Set the selectedItem here for the column "Currency"
...
}
我找到了很多关于为普通 ComboBoxes 设置所选项目的教程,但不是为 DataGridComboBoxColumns 设置的。我已经用currencies.SetCurrentValue() 试过了,但是我从DataGridComboBoxColumn 找不到合适的DependencyProperty。
有人可以帮帮我吗?
提前致谢。
博尔迪
【问题讨论】:
标签: c# wpf datagrid observablecollection datagridcomboboxcolumn