我创建了一个DataGrid
并以DataGridComboBoxColumn
编程方式添加了一个。
public partial class MainWindow : Window
{
private DataGridComboBoxColumn weightColumnChar = new DataGridComboBoxColumn();
ObservableCollection<int> mComboBoxValues;
public ObservableCollection<int> ComboBoxValues
{
get { return this.mComboBoxValues; }
set { this.mComboBoxValues = value; }
}//end property
public MainWindow()
{
InitializeComponent();
mComboBoxValues = new ObservableCollection<int>() {-1, 0, 1 };
}//end constructor
private void Window_Loaded(object sender, RoutedEventArgs e)
{
weightColumnChar.Header = "Weight";
dataGrid_Char.Columns.Add(weightColumnChar);
weightColumnChar.ItemsSource = ComboBoxValues;
Binding binding = new Binding();
binding.Path = new PropertyPath(ComboBoxValues[1]);
weightColumnChar.SelectedItemBinding = binding;
}//end method
private void dataGrid_Char_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}//end method
//Opens ComboBox on first click
private void dataGrid_Char_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(DataGridCell))
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
}//end if
}//end method
}//end class
我在其中添加了一个ItemsSource
并从ObservableCollection
.
集合中的值在运行时显示。
我的问题是,如果我从该值中选择一个值,ComboBox
则不会选择并在之后显示。我究竟做错了什么?
而且我还想选择一个默认值。这是如何运作的?
请以编程方式而不是在 XAML 中解释!
如果有人可以帮助我,那就太好了。
谢谢!!!