Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项

2022-10-16,,,,

场景

winform中对zedgraph的radiogroup进行数据源绑定,即通过代码添加选项

https://blog.csdn.net/badao_liumang_qizhi/article/details/100540152

winform中自定义xml配置文件后对节点进行读取与写入:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100532137

结合上面两种效果实现打开一个新的窗体后,此窗体上的radiogroup的选项是根据配置文件

中的配置自动生成的。

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
大量编程视频教程:

配置文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<configure>
  <!--y轴集合-->
  <yaxis>
    <!--第一条y轴-->
    <yaxi>
      <no>1</no>
      <title>霸道</title>
      <color>black</color>
      <min>-1500</min>
      <max>1500</max>
    </yaxi>
    <!--第二条y轴-->
    <yaxi>
      <no>2</no>
      <title>电压</title>
      <color>black</color>
      <min>-1500</min>
      <max>1500</max>
    </yaxi>
    <yaxi>
      <no>3</no>
      <title>badao</title>
      <color>red</color>
      <min>-1600</min>
      <max>1600</max>
    </yaxi>
  </yaxis>
</configure>

 

实现

新建一个窗体并拖拽一个radiogroup控件。

 

 

双击窗体进入其加载完之后的事件中

 

 private void edity_load(object sender, eventargs e)
        {
            list<yaxismodel> nodeylist = new list<yaxismodel>();
            //获取可执行文件的路径-即bin目录下的debug或者release目录
            string context = system.windows.forms.application.startuppath;
            string path = string.concat(context,@"\config\yaxisset.xml"); 
            xmldocument xml = new xmldocument();
            //打开一个xml
            try
            {
                xml.load(path);
                //读取节点数据
                xmlnodelist nodelist = xml.selectnodes("configure/yaxis/yaxi");
                foreach (xmlnode n in nodelist)
                {
                   yaxismodel ya = new yaxismodel();
                   ya.no = int.parse(n.selectsinglenode("no").innertext);
                   ya.title = n.selectsinglenode("title").innertext;
                   ya.color = n.selectsinglenode("color").innertext;
                   ya.min = double.parse(n.selectsinglenode("min").innertext);
                   ya.max = double.parse(n.selectsinglenode("max").innertext);
                   nodeylist.add(ya);
                }
            }
            catch (exception ex)
            {
                console.writeline(ex.message);
            }
            
            //数据绑定
            foreach (yaxismodel s in nodeylist)
            {
                //每一个单元按钮对应的选线item
                radiogroupitem item = new radiogroupitem();
                //设置选项的value值
                item.value = s.no;
                //设置选项的描述值 即 要显示的值
                item.description = s.title;
                //使选项启用
                item.enabled = true;
                //将新增的选项添加到radiogroup的items中
                this.radiogroup1.properties.items.add(item);
            }
           //默认选中value为1的项 
            radiogroup1.editvalue = 1;
         }

 

在此之前要新建一个对象用来存取读取的配置文件的yaxi节点的属性。

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace zedgraphtest.model
{
    class yaxismodel
    {
        private int no;

        public int no
        {
            get { return no; }
            set { no = value; }
        }
        private string title;

        public string title
        {
            get { return title; }
            set { title = value; }
        }
        private string color;

        public string color
        {
            get { return color; }
            set { color = value; }
        }
        private double min;

        public double min
        {
            get { return min; }
            set { min = value; }
        }
        private double max;

        public double max
        {
            get { return max; }
            set { max = value; }
        }
    }
}

 

效果

 

《Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项.doc》

下载本文的Word格式文档,以方便收藏与打印。