如何将XML数据填充到组合框?

时间:2017-07-10 03:42:16

标签: c# xml wpf combobox datagrid

我有一个像这样的Xml文件:

<NewDataSet>
  <Communications>
     <ModelNumber>0x01</ModelNumber>
     <ParamName>BaudRate</ParamName>
     <ParamValue>19200</ParamValue>
     <DefaultValue>502</DefaultValue>
     <MaxValue></MaxValue>
     <MinValue></MinValue>
  </Communications>
</NewDataSet>

我曾经通过DataSet将xml绑定到我的数据网格,如下所示:

    public ObservableCollection<Communication> GetCommunications()
    {
        DataSet ds = StoreDbDataSet.ReadDataSet();
        ObservableCollection<Communication> communications = new ObservableCollection<Communication>();
        foreach (DataRow communicationRow in ds.Tables["Communications"].Rows)
        {
            communications.Add(new Communication((UInt16)communicationRow["ModelNumber"], communicationRow["ParamName"].ToString(),
                    communicationRow["ParamValues"].ToString(), communicationRow["DefaultValue"].ToString(), communicationRow["MaxValue"].ToString(),
                    communicationRow["MinValue"].ToString()));
        }
        return communications;
    }

    public ICollectionView CommunicationsView
    {
        get
        {
            if (_CommunicationsView == null)
                RefreshCommunication();
            return _CommunicationsView;
        }
        set
        {
            _CommunicationsView = value;
            NotifyPropertyChanged();
        }
    }

    private void RefreshCommunication()
    {
        CommunicationsView = new ListCollectionView(sdb.GetCommunications())
        {
            Filter = obj =>
            {
                var communication = (Communication)obj;
                return SelectedProduct != null && SelectedProduct.ModelNumber == communication.ModelNumber;
            }
        };
    }

    <DataGrid Name="dgCommunication" ItemsSource="{Binding CommunicationsView}" BorderThickness="5" AutoGenerateColumns="False">

现在我想像这样更改我的Xml文件:

<NewDataSet>
  <Communications>
    <ModelNumber>1</ModelNumber>
    <ParamName>BaudRate</ParamName>
    <ParamValues>
      <ParamValue>9600</ParamValue>
      <ParamValue>19200</ParamValue>
      <ParamValue>115200</ParamValue>
    </ParamValues>
    <DefaultValue>19200</DefaultValue>
    <MaxValue></MaxValue>
    <MinValue></MinValue>
  </Communications>
  <Communications>
    <ModelNumber>1</ModelNumber>
    <ParamName>Parity</ParamName>
    <ParamValues>
      <ParamValue>None</ParamValue>
      <ParamValue>Odd</ParamValue>
      <ParamValue>Even</ParamValue>
    </ParamValues>
    <DefaultValue>None</DefaultValue>
    <MaxValue></MaxValue>
    <MinValue></MinValue>
  </Communications>
  <Communications>
    <ModelNumber>1</ModelNumber>
    <ParamName>StopBit</ParamName>
    <ParamValues>
      <ParamValue>1</ParamValue>
      <ParamValue>2</ParamValue>
    </ParamValues>
    <DefaultValue>1</DefaultValue>
    <MaxValue></MaxValue>
    <MinValue></MinValue>
  </Communications>
  <Communications>
    <ModelNumber>1</ModelNumber>
    <ParamName>DataBit</ParamName>
    <ParamValues>
      <ParamValue>7</ParamValue>
      <ParamValue>8</ParamValue>
    </ParamValues>
    <DefaultValue>8</DefaultValue>
    <MaxValue></MaxValue>
    <MinValue></MinValue>
  </Communications>
  <Communications>
    <ModelNumber>1</ModelNumber>
    <ParamName>SlaveAddress</ParamName>
    <ParamValues>
      <ParamValue>1</ParamValue>
    </ParamValues>
    <DefaultValue>1</DefaultValue>
    <MaxValue>247</MaxValue>
    <MinValue>1</MinValue>
  </Communications>
</NewDataSet>

因此我可以在我的数据网格中使用组合框,我应该如何更改当前代码来修复此问题?提前感谢!

修改

public class Communication : 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 string maxValue;
    public string MaxValue
    {
        get { return maxValue; }
        set
        {
            maxValue = value;
            OnPropertyChanged(new PropertyChangedEventArgs("MaxValue"));
        }
    }

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

    public Communication(UInt16 modelNumber, string paramName, string paramValue, string defaultValue, string maxValue, string minValue)
    {
        ModelNumber = modelNumber;
        ParamName = paramName;
        ParamValue = paramValue;
        DefaultValue = defaultValue;
        MaxValue = maxValue;
        MinValue = minValue;
    }

    private ObservableCollection<Communication> communications;
    public ObservableCollection<Communication> Communications
    {
        get { return communications; }
        set { communications = value; }
    }


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

更新

public class StoreDbDataSet
{
    private const string DatabaseFileName = "store.xml";
    private const string DatabaseConstructorName = "store.xsd";


    public static DataSet ReadDataSet()
    {
        DataSet ds = new DataSet();
        ds.ReadXmlSchema(DatabaseConstructorName);
        ds.ReadXml(DatabaseFileName);
        ds.AcceptChanges();
        return ds;
    }
}

Picture of xsd here

1 个答案:

答案 0 :(得分:1)

List<string>班级添加Communication媒体资源:

private readonly List<string> _paramValues = new List<string>();
public List<string> ParamValues { get { return _paramValues; } }

修改GetCommunications()方法:

public ObservableCollection<Communication> GetCommunications()
{
    DataSet ds = StoreDbDataSet.ReadDataSet();
    ObservableCollection<Communication> communications = new ObservableCollection<Communication>();
    foreach (DataRow communicationRow in ds.Tables["Communications"].Rows)
    {
        var c = new Communication((ushort)Convert.ToInt16(communicationRow["ModelNumber"]), communicationRow["ParamName"].ToString(),
                ds.Tables["ParamValue"].Rows[0][0].ToString(), communicationRow["DefaultValue"].ToString(), communicationRow["MaxValue"].ToString(),
                communicationRow["MinValue"].ToString());
        foreach (DataRow dr in ds.Tables["ParamValue"].Rows)
        {
            c.ParamValues.Add(dr[0].ToString());
        }
        communications.Add(c);
    }
    return communications;
}

DataGridComboBoxColumn的{​​{1}}集合中添加DataGrid

Columns