代码中的datagridcomboboxcolumn绑定
datagridcomboboxcolumn binding in code
我想完全用代码来完成,不 XAML:
给定
DataGridComboBoxColumn myDGCBC = new DataGridComboBoxColumn();
ObservableCollection<string> DataSource = new ObservableCollection<string>{"Option1", "Option2"};
myDGCBC.ItemsSource = DataSource;
ObservableCollection<MyStructure> MyObject = new ObservableCollection<MyStructure>;
和
public class MyStructure
{
... several properties ... // pseudocode, obviously
public string SelectedValue { get; set; }
}
我有兴趣(绑定)从列中的所有组合框接收选定值到 SelectedValue
属性。
我尝试了 SO 的几个想法,但无济于事。
求助!
谢谢
假设 DataGird
已经在 xaml 中定义,您应该为 DataGrid
和 DataGridComboBoxColumn
设置适当的绑定。
举个例子给大家一个思路:
Xaml:
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid x:Name="myGrid" AutoGenerateColumns="False"/>
<Button Grid.Row="1" Content="test" Click="Button_Click"/>
</Grid>
MainWindow.cs:
//DataGrid ItemsSource
public ObservableCollection<MyStructure> DataSource { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
// Initializing DataGrid.ItemsSource
DataSource = new ObservableCollection<MyStructure>();
DataSource.Add(new MyStructure());
// Creating new DataGridComboBoxColumn
DataGridComboBoxColumn myDGCBC = new DataGridComboBoxColumn();
myDGCBC.Header = "cmbColumn";
// Binding DataGridComboBoxColumn.ItemsSource and DataGridComboBoxColumn.SelectedItem
var cmbItems = new ObservableCollection<string> { "Option1", "Option2" };
myDGCBC.ItemsSource = cmbItems;
myDGCBC.SelectedItemBinding = new Binding("SelectedValue");
// Adding DataGridComboBoxColumn to the DataGrid
myGrid.Columns.Add(myDGCBC);
// Binding DataGrid.ItemsSource
Binding binding = new Binding();
binding.Source = DataSource;
BindingOperations.SetBinding(myGrid, DataGrid.ItemsSourceProperty, binding);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//This is just to check whether SelectedValue is set properly:
string selectedValue = DataSource[0].SelectedValue;
}
我想完全用代码来完成,不 XAML: 给定
DataGridComboBoxColumn myDGCBC = new DataGridComboBoxColumn();
ObservableCollection<string> DataSource = new ObservableCollection<string>{"Option1", "Option2"};
myDGCBC.ItemsSource = DataSource;
ObservableCollection<MyStructure> MyObject = new ObservableCollection<MyStructure>;
和
public class MyStructure
{
... several properties ... // pseudocode, obviously
public string SelectedValue { get; set; }
}
我有兴趣(绑定)从列中的所有组合框接收选定值到 SelectedValue
属性。
我尝试了 SO 的几个想法,但无济于事。
求助! 谢谢
假设 DataGird
已经在 xaml 中定义,您应该为 DataGrid
和 DataGridComboBoxColumn
设置适当的绑定。
举个例子给大家一个思路:
Xaml:
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid x:Name="myGrid" AutoGenerateColumns="False"/>
<Button Grid.Row="1" Content="test" Click="Button_Click"/>
</Grid>
MainWindow.cs:
//DataGrid ItemsSource
public ObservableCollection<MyStructure> DataSource { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
// Initializing DataGrid.ItemsSource
DataSource = new ObservableCollection<MyStructure>();
DataSource.Add(new MyStructure());
// Creating new DataGridComboBoxColumn
DataGridComboBoxColumn myDGCBC = new DataGridComboBoxColumn();
myDGCBC.Header = "cmbColumn";
// Binding DataGridComboBoxColumn.ItemsSource and DataGridComboBoxColumn.SelectedItem
var cmbItems = new ObservableCollection<string> { "Option1", "Option2" };
myDGCBC.ItemsSource = cmbItems;
myDGCBC.SelectedItemBinding = new Binding("SelectedValue");
// Adding DataGridComboBoxColumn to the DataGrid
myGrid.Columns.Add(myDGCBC);
// Binding DataGrid.ItemsSource
Binding binding = new Binding();
binding.Source = DataSource;
BindingOperations.SetBinding(myGrid, DataGrid.ItemsSourceProperty, binding);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//This is just to check whether SelectedValue is set properly:
string selectedValue = DataSource[0].SelectedValue;
}