如何从数据网格组合框列中获取所选项目值

时间:2015-07-14 21:06:11

标签: c# wpf xaml datagrid combobox

我有一个带有文本列和组合框列的数据网格。

数据网格列如下:

<DataGrid.Columns>
    <DataGridTextColumn Header="ID"  
                        Binding="{Binding ID}" 
                        Width="0.5*" />
    <DataGridComboBoxColumn Header="Threshold"  
                            x:Name="Threshold" 
                            SelectedItemBinding="{SelectedValue}"/>
</DataGrid.Columns>

对于ComboBox列,我使用List作为ItemSource。

Threshold.ItemsSource = MaxThreshold; //MaxThreshold is a List

因此每一行都有一个ComboBox,它由List值组成。

用户选择特定行,然后选择该特定行中的组合框。 我想获得在c#中的ComboBox中选择的项目的值。

感谢。

2 个答案:

答案 0 :(得分:2)

要完成您想要的工作,首先需要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);
        }
    }

注意:您必须为其认为必要的类型更改类型的属性和属性

您的DataGrid类似于下面显示的XAMLproperties填充DataGridComboBox位于.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>

Window .cs文件中,我们有以下代码段:

 public List<MyModel> MyDataGridItems { get; set; }
 public List<object> MaxThreshold { get; set; }

在窗口constructor中,您可以初始化列表:

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"
};

要在DataGrid的特定selected item中获取combobox row,您可以遍历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

答案 1 :(得分:0)

这是我的例子:

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>                                

代码背后的

我拥有自己的财产:

private ObservableCollection<DGFeature> _BarDetail;
    public ObservableCollection<DGFeature> BarDetail
    {
        get { return _BarDetail; }
        set
        {
            if (_BarDetail != value)
            {
                _BarDetail = value;
                OnPropertyChanged("BarDetail");
            }
        }
    }

我创建了一个用于填充数据网格的类:

  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用于在组合框中设置所选项目