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

时间:2023-11-10 15:45:56

场景

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的选项是根据配置文件

中的配置自动生成的。

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

配置文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<Configure>
<!--Y轴集合-->
<YAxis>
<!--第一条Y轴-->
<YAxi>
<no></no>
<title>霸道</title>
<color>black</color>
<min>-</min>
<max></max>
</YAxi>
<!--第二条Y轴-->
<YAxi>
<no></no>
<title>电压</title>
<color>black</color>
<min>-</min>
<max></max>
</YAxi>
<YAxi>
<no></no>
<title>badao</title>
<color>red</color>
<min>-</min>
<max></max>
</YAxi>
</YAxis>
</Configure>

实现

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

Winform中实现读取xml配置文件并动态配置ZedGraph的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 = ;
}

在此之前要新建一个对象用来存取读取的配置文件的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的选项