组合框在一行中包含多个变量,保存选择

时间:2015-06-29 14:05:41

标签: c# xml winforms linq

我正在使用XML,过滤并将其放入组合框中,我的问题是选择组合框输入,并保存每个单独的部分。这是一些使用XML的示例。

<SolutionString>  
    <Solutions>
        <Solution>
          <ID>1</ID>
          <Property>
            <Name>DriverSheave</Name>
            <Value>1VP34</Value>
          </Property>
          <Property>
            <Name>DriverBushing</Name>
            <Value>
            </Value>
          </Property>
          <Property>
            <Name>DrivenSheave</Name>
            <Value>AK49</Value>
          </Property>
          <Property>
            <Name>DrivenBushing</Name>
            <Value>
            </Value>
          </Property>
        </Solution>
        <Solution>
          <ID>2</ID>

...等(ID 2,ID 3等)。在此之后我将这些XML结果粘贴到我的组合框中。

XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(XmlString.ToString());
PTS.Library.VbeltDriveLibrary.Configurator Configurator = new PTS.Library.VbeltDriveLibrary.Configurator(doc1);
if (Configurator.SolveAndValidate())
{
    var solutions = Configurator.Results.ToXDocument();
    int i = 0;
    var indexesToChoose = new List<int> { 9, 8, 4, 5, 0, 2, 7, 6 };
    var cat = solutions
        .Descendants("Solution")
        .Select(x => new
        {
            ID = (string)x.Element("ID"),
            Properties = x.Elements("Property").Select(p => new
            {
                Name = (string) p.Element("Name"),
                Value = (string) p.Element("Value"),

                idx = (i < 11 ? i++ : i = 0)
            })
            .Where(y => indexesToChoose.Contains(y.idx))
            .OrderBy(z => indexesToChoose.FindIndex(p => p == z.idx))
            .ToList()
        });

    var items = cat
        .Select(s => new
        {
            ID = s.ID,
            Text = string.Format("{0}. {1}", s.ID,
            string.Join(", ", s.Properties
                               .Select(p => string.Format("{0} = {1}",
                                   p.Name,
                                   p.Value ?? "(null)"))))
        }).ToArray();
    comboBox1.DisplayMember = "Text";
    comboBox1.ValueMember = "ID";
    comboBox1.Items.AddRange(items);

最后,我希望能够选择这个(这是组合框中的选定项目)

  
      
  1. 成本= 1072.93,ActualDrivenShaftSpeed = 900/1073,Belt = B84,BeltQty = 5,DriverSheave = 5MVP70B84P,Comment2 =此驱动器的正确张力(1.31磅应偏转皮带0.48英寸)将有30磅。&# 39;运行&#39;轮毂负载,DrivenSheave = 5MVB70R,ActualServiceFactor = 40.63,ActualCenterDistance = 30.8
  2.   

并将每个片段过滤成变量,例如

string ActualCenterDistance = 30.8

显然,我可以使用简单的combobox.selectedText轻松地将整个选择放入字符串中,但是将每个部分放入单个字符串(或任何其他var)是我的问题。

1 个答案:

答案 0 :(得分:1)

对我来说,这看起来很糟糕。

请考虑在此处使用匿名类型:

.Select(x => new
            {
                ID = (string)x.Element("ID"),
                Properties = x.Elements("Property").Select(p => new

只需创建自己的类的实例,可以是:

public class MyItem
{
    public string ID;
    public List<Tuple<string, string>> Properties;

    public string GetProperty(string name)
    {
        if (Properties == null)
            return null;

        var item = Properties.FirstOrDefault(x => x.Item1 == name);
        return item == null ? null : item.Item2;
    }

    public override string ToString()
    {
        return string.Join(", ", Properties
            .Select(p => string.Format("{0} = {1}",
                p.Item1,
                p.Item2 ?? "(null)")));
    }
}

在这种情况下(因为我已经将连接名称/属性值的逻辑转移到ToString类的MyItem方法覆盖) - 您可以使用类型为{{1}的项填充组合框},您将能够轻松访问所有数据:

MyItem

使用代码进行更改:

var item = comboBox1.SelectedItem as MyItem;
string x = item.GetProperty("DriverSheave");

注意:对于组合框有一些自定义类var items = solutions.Descendants("Solution") .Select(x => new MyItem { ID = (string)x.Element("ID"), Properties = x.Elements("Property").Select(p => new { Name = (string)p.Element("Name"), Value = (string)p.Element("Value"), idx = (i < 11 ? i++ : i = 0) }) .Where(y => indexesToChoose.Contains(y.idx)) .OrderBy(z => indexesToChoose.FindIndex(p => p == z.idx)) .Select(z => new Tuple<string, string>(z.Name, z.Value)) .ToList() }).ToArray(); comboBox1.DisplayMember = "Text"; comboBox1.ValueMember = "ID"; comboBox1.Items.AddRange(items); 非常重要,因为在使用匿名类型的情况下,从Item获取对象时将无法访问其字段