如何在datagrid中获得组合框选择的值?

时间:2017-07-13 08:17:11

标签: c# wpf combobox datagrid

我在combobox中有一个datagrid,如下所示:

<DataGrid Name="dgConfig" AutoGenerateColumns="False" ItemsSource="{Binding ModulesView}" >
     <DataGrid.Columns>
          <DataGridTextColumn Header="ParamName" Binding="{Binding ParamName}" IsReadOnly="True"/>
          <DataGridTemplateColumn Header="ParamValues">
              <DataGridTemplateColumn.CellTemplate>                                                                 
                   <DataTemplate>                                                           
                       <ComboBox ItemsSource="{Binding ParamValues}" SelectedItem="{Binding DefaultValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}}" />
                   </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>                                                           
         </DataGridTemplateColumn>
         <DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" IsReadOnly="True"/>
     </DataGrid.Columns>
</DataGrid>

现在我想在后面的代码中获得列ParamValues中所有选定的组合框值,所以我尝试了这个:

var temp = ModulesView.Cast<Module>().Select(p => p.ParamValues).ToArray();

修改

    private ICollectionView _ModulesView;
    public ICollectionView ModulesView
    {
        get
        {
            if (_ModulesView == null)
                RefreshModules();
            return _ModulesView;
        }
        set
        {
            _ModulesView = value;
            NotifyPropertyChanged();
        }
    }

    private ObservableCollection<Module> myModulesList;   
    private void RefreshModules()
    {
        myModulesList = new ObservableCollection<Module>(GetModules().OrderBy(mod => mod.StartAddress));
        ModulesView = CollectionViewSource.GetDefaultView(myModulesList);
        ModulesView.Filter = obj =>

        {
            var modules = (Module)obj;
            return SelectedProduct != null && SelectedProduct.ModelNumber == modules.ModelNumber;
        };

    }

    public ObservableCollection<Module> GetModules()
    {
        ObservableCollection<Module> modules = new ObservableCollection<Module>();
        XDocument doc = XDocument.Load("Modules.xml");
        foreach (XElement moduleRow in doc.Root.Elements("Modules"))
        {
            var c = new Module((ushort)Convert.ToInt16(moduleRow.Element("ModelNumber").Value), moduleRow.Element("ParamName").Value,
                    moduleRow.Element("ParamValues").Value, moduleRow.Element("DefaultValue").Value, (ushort)Convert.ToInt16(moduleRow.Element("StartAddress").Value),
                    (ushort)Convert.ToInt16(moduleRow.Element("StartBit").Value), (ushort)Convert.ToInt16(moduleRow.Element("Length").Value));
            foreach (XElement paramValue in moduleRow.Element("ParamValues").Elements())
            {
                c.ParamValues.Add(paramValue.Value);
            }
            modules.Add(c);
        }
        return modules;
    }

和模块:

public class Module : INotifyPropertyChanged
{
    private UInt16 modelNumber;
    public UInt16 ModelNumber
    {
        get { return modelNumber; }
        set
        {
            modelNumber = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ModelNumber"));
        }
    }

    private string paramName;
    public string ParamName
    {
        get { return paramName; }
        set
        {
            paramName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ParamName"));
        }
    }

    private string paramValue;
    public string ParamValue
    {
        get { return paramValue; }
        set
        {
            paramValue = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ParamValue"));
        }
    }

    private string defaultValue;
    public string DefaultValue
    {
        get { return defaultValue; }
        set
        {
            defaultValue = value;
            OnPropertyChanged(new PropertyChangedEventArgs("DefaultValue"));
        }
    }

    private List<string> _paramValues = new List<string>();
    public List<string> ParamValues
    {
        get { return _paramValues; }
        set
        {
            _paramValues = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ParamValues"));
        }
    }

    private UInt16 startAddress;
    public UInt16 StartAddress
    {
        get { return startAddress; }
        set
        {
            startAddress = value;
            OnPropertyChanged(new PropertyChangedEventArgs("StartAddress"));
        }
    }


    private UInt16 startBit;
    public UInt16 StartBit
    {
        get { return startBit; }
        set
        {
            startBit = value;
            OnPropertyChanged(new PropertyChangedEventArgs("StartBit"));
        }
    }

    private UInt16 length;
    public UInt16 Length
    {
        get { return length; }
        set
        {
            length = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Length"));
        }
    }

    public Module(UInt16 modelNumber, string paramName, string paramValue, string defaultValue, UInt16 startAddress, UInt16 startBit, UInt16 length)
    {
        ModelNumber = modelNumber;
        ParamName = paramName;
        ParamValue = paramValue;
        DefaultValue = defaultValue;
        StartAddress = startAddress;
        StartBit = startBit;
        Length = length;
    }

    private ObservableCollection<Module> modules;
    public ObservableCollection<Module> Modules
    {
        get { return modules; }
        set { modules = value; }
    }

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

但是这个返回的ParamValues列中的组合框的所有值都不是所选的。我该怎么解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:1)

您从DefaultValue源集合中的每个Communication对象的ObservableCollection<Communication>属性中获取当前选定的值,因为您绑定了SelectedItem ComboBox属性1}}到这个属性:

var selectedValues = ModulesView.Cast<Module>().Select(p => p.DefaultValue.ToArray());

您可能还希望将绑定的UpdateSourceTrigger属性设置为PropertyChanged,以便立即设置源属性:

<ComboBox ItemsSource="{Binding ParamValues}" SelectedItem="{Binding DefaultValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

答案 1 :(得分:0)

<DataGrid Name="dgConfig" AutoGenerateColumns="False" ItemsSource="{Binding ModulesView}" >
 <DataGrid.Columns>
      <DataGridTextColumn Header="ParamName" Binding=" {Binding ParamName}" IsReadOnly="True"/>
      <DataGridTemplateColumn Header="ParamValues">
          <DataGridTemplateColumn.CellTemplate>                                                                 
               <DataTemplate>                                                           
                   <ComboBox ItemsSource="{Binding ParamValues}" SelectedItem="{Binding DefaultValue, Mode=TwoWay}" SelectedValuePath="ParamValues" SelectedValue="{Binding ParamValues,Mode=TwoWay}" />
               </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>                                                           
     </DataGridTemplateColumn>
     <DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" IsReadOnly="True"/>
 </DataGrid.Columns>

现在,当一个事件将触发值选择时,您将在对象中选择值。请参阅上面的代码更改。我添加了selectedvalue&amp;组合框的selectedvaluepath属性。