使用c#在特定标签下创建动态9复选框3

时间:2016-05-17 06:21:23

标签: c# xml winforms

enter image description here enter image description here我想在Windows应用程序中添加复选框(见下图)。任何人都可以帮我解决这个问题吗?复选框标签已从xml绑定。我已添加了xml结构。我无法理解如何从中开始。

3 个答案:

答案 0 :(得分:1)

我不了解您的XML组织,但我希望它会有所帮助。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
         XmlDocument Doc = new XmlDocument();
         Doc.Load("YourPath to XML document");
         foreach (XmlNode node in Doc.DocumentElement.ChildNodes)
         {
             if (node.Name == "perrent")
             {
                 /*This code setup name of label, we take Attribute of parrent*/
                 Label lb = new Label();
                 lb.Content = node.Attributes.GetNamedItem("ParrentAttribute");
                 /*SPanel is added in MainWindow XAML it  is a ordinary Stack Panel you can dynamically create stack panel and just add to grid from code*/
                 SPanel.Children.Add(lb);
                 XmlNodeList SearchNode = node.ChildNodes;
                 /*Listing all child nodes to create check boxes */
                 foreach (XmlNode Child in SearchNode)
                 {
                     CheckBox cb = new CheckBox();
                     cb.Margin = new Thickness(20, 0, 0, 0);
                     cb.Content = Child.InnerText;
                     SPanel.Children.Add(cb);

                 }
             }
         }
    }
}

XAML

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel x:Name="SPanel"/>
</Grid>

XML文档

<root>
  <perrent ParrentName = "Dev">
    <node>File import Service</node>
    <node>Settlement Service</node>
    <node>Store conversion Service</node>
  </perrent>
   <perrent ParrentName = "QA">
     <node>File import Service</node>
    <node>Settlement Service</node>
    <node>Store conversion Service</node>
  </perrent>
</root>

答案 1 :(得分:1)

请尝试使用下面的C#代码:

try
        {
            XmlReader xmlFile;
            xmlFile = XmlReader.Create("E:\\Product.xml", new XmlReaderSettings());
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            var TblTitle = ds.Tables[0];
            var i = 39;
            var j = 67;

            foreach(DataRow row in TblTitle.Rows)
            {
                //Create new GroupBox
                GroupBox GrpBox = new GroupBox();
                GrpBox.Text = row.ItemArray[1].ToString();
                GrpBox.Location = new System.Drawing.Point(i, j);

                //Create new Checkboxlist in GroupBox
                CheckedListBox ChkList = new CheckedListBox();
                ChkList.Location = new System.Drawing.Point(44, 20);
                ChkList.DataSource = TblTitle.ChildRelations[0].ChildTable;
                ((ListBox)ChkList).ValueMember = "Name";
                ((ListBox)ChkList).DisplayMember = "Name";

                //add Checkboxlist into GroupBox

                GrpBox.Controls.Add(ChkList);

                //add this groupBox to Form
                this.Controls.Add(GrpBox);

                //set position for next one
                i = +1;
                j = +176;
            }


            //checkedListBox1.
            //checkedListBox1.DataBindings();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

和这个xml示例:

<?xml version="1.0" encoding="utf-8" ?>
<CheckBox Name="Dev">
  <item Name="item1"/>
  <item Name="item2"/>
</CheckBox>
<CheckBox Name="QA">
  <item Name="item3"/>
  <item Name="item4"/>
</CheckBox>

如果您有任何疑虑,请告诉我。我会举例说明。

答案 2 :(得分:1)

`private void bindChkServerIIS()         {             列表chkListIIS = new List();             chkListIIS = Serverlist();

        int x = 50;
        int y = 0;
        int p = 240;
        foreach (ItemLists serviceItem in chkListIIS)
        {
            y = y + 18;
            string itemValue = serviceItem.Value;
            string itemName = serviceItem.Name;

            CheckBox box1 = new CheckBox();
            Label lbl = new Label();
            box1.Text = itemName;
            box1.Font = new Font(FontFamily.GenericSansSerif, 8F);
            lbl.Font = new Font(FontFamily.GenericSansSerif, 8F);
            //lbl.Text = CheckStatus(itemName, itemValue);
            box1.Tag = itemValue;
            lbl.Text = "Status";
            lbl.AutoSize = true;
            box1.AutoSize = true;
            gbIis.Controls.Add(box1);
            gbIis.Controls.Add(lbl);
            box1.Location = new Point(x, y);
            lbl.Location = new Point(p, y);
        }

    }` private List<ItemLists> Serverlist()
    {
        xmldoc.Load(xmlPath);
        XmlNodeList nodelist = xmldoc.SelectNodes("servers");
        List<ItemLists> chkListServer = new List<ItemLists>();
        {
            foreach (XmlNode node in nodelist.Item(0))
            {
                foreach (XmlNode childElement in node)
                {

                    string serverApp = childElement.ChildNodes[4].InnerText;
                    string serverName = childElement.Attributes["Name"].Value;

                    if (childElement.Attributes["Name"].Value != "vmxxhegepda01"
                        && childElement.Attributes["Name"].Value != "vmxxhegepqa01"
                        && childElement.Attributes["Name"].Value != "vmxxhegeppa01")
                    {
                        chkListServer.Add(new ItemLists() { Name = serverName, Value = serverApp });
                    }

                }

            }
        }
        return chkListServer;
    }`enter code here`