wpf 如何从数据网格组合框列中获取所选项目值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31417386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the Selected Item Value from Data Grid Combo Box Column
提问by Abhishek
I have a Data Grid With a text column and a Combo Box Column.
我有一个带有文本列和组合框列的数据网格。
The Data Grid Columns is as Follows:
数据网格列如下:
<DataGrid.Columns>
<DataGridTextColumn Header="ID"
Binding="{Binding ID}"
Width="0.5*" />
<DataGridComboBoxColumn Header="Threshold"
x:Name="Threshold"
SelectedItemBinding="{SelectedValue}"/>
</DataGrid.Columns>
For the ComboBox Column I am using a List as an ItemSource.
对于 ComboBox 列,我使用列表作为 ItemSource。
Threshold.ItemsSource = MaxThreshold; //MaxThreshold is a List
So every row has a ComboBox which consists of the List values.
所以每一行都有一个由 List 值组成的 ComboBox。
The user selects a particular row and then selects the combo box in that particular row. I want to get the value of the item selected in the ComboBox in c#.
用户选择特定行,然后选择该特定行中的组合框。我想在 C# 中获取在 ComboBox 中选择的项目的值。
Thanks.
谢谢。
回答by Nicolas Dias
To do the job you want you first need a data model, that is, in your case this model must contain two properties, and this model implements the INotifyPropertyChangedinterface to notify the UIthat property values They have changed. Below is the modelthat fits in your example, I called the model MyModel:
要完成您想要的工作,您首先需要一个data model,也就是说,在您的情况下,此模型必须包含两个properties,并且此模型实现了INotifyPropertyChanged接口以通知该UI属性值已更改。下面是model适合你的例子,我称之为模型MyModel:
public class MyModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private object _id;
public object ID
{
get { return _id; }
set
{
_id = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("ID"));
}
}
private object _selectedItem;
public object SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
}
}
public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
}
Note:You must change the type of attributes and propertiesfor the type it deems necessary.
注意:您必须更改它认为必要的类型的属性和属性类型。
Your DataGridwould be something like the XAMLshown below (the propertiesthat fill the DataGridand the ComboBoxare in the .cs fileof the window):
您DataGrid将是XAML如下所示的内容(properties填充 的DataGrid和ComboBox位于 的.cs file中window):
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False"
ItemsSource="{Binding MyDataGridItems,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
<DataGrid.Columns >
<DataGridTextColumn Header=" ID"
Binding="{Binding ID}"
Width="0.5*" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="Threshold"
SelectedItem="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding MaxThreshold,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
In Window.cs filewe have the following code snippet:
在Window.cs 文件中,我们有以下代码片段:
public List<MyModel> MyDataGridItems { get; set; }
public List<object> MaxThreshold { get; set; }
In the constructorof Window you can initialize the lists:
在constructorWindow 中,您可以初始化列表:
myDataGridItems = new List<MyModel>()
{
new MyModel(){ID=1},
new MyModel(){ID=2},
new MyModel(){ID=3},
new MyModel(){ID=4},
new MyModel(){ID=5},
};
MaxThreshold = new List<object>()
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};
And to pick up the selected itemof comboboxin the particular rowof DataGrid you can iterate through the items of the DataGrid, or access the desired row directly, is an example:
并拿起selected item的combobox在特定的rowDataGrid中,你可以通过DataGrid,或直接访问所需的行中的项目迭代,是一个例子:
//iterate rows
foreach(MyModel model in myDataGrid.Items)
{
var selecteditem = model.SelectedItem;//here you have selected item
}
//access directly
var model = myDataGrid.Items[0] as MyModel;
if(model!=null)
var selecteditem = model.SelectedItem;//here you have selected item
回答by Bianca Kalman
Here is my exemple:
这是我的例子:
XAML code:
XAML 代码:
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FeatureName}" />
<DataGridCheckBoxColumn Binding="{Binding FeatureExists}" />
<DataGridComboBoxColumn DisplayMemberPath="MachineID" SelectedItemBinding="{Binding SelectedMchn}">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=FeatureMachines}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=FeatureMachines}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
in code behind I have my property:
在后面的代码中,我有我的财产:
private ObservableCollection<DGFeature> _BarDetail;
public ObservableCollection<DGFeature> BarDetail
{
get { return _BarDetail; }
set
{
if (_BarDetail != value)
{
_BarDetail = value;
OnPropertyChanged("BarDetail");
}
}
}
I have created a class for populating the datagrid:
我创建了一个用于填充数据网格的类:
public class DGFeature
{
public string FeatureName { get; set; }
public bool FeatureExists { get; set; }
public List<LibTrackingSystem.Machine> FeatureMachines { get; set; }
public LibTrackingSystem.Machine SelectedMchn { get; set; }
public DGFeature()
{
FeatureMachines = new List<LibTrackingSystem.Machine>();
SelectedMchn = new LibTrackingSystem.Machine();
}
}
SelectedMchn is used to set the selected item in the combo box
SelectedMchn 用于设置组合框中的选中项

