C#窗体重启前将radiobutton属性保存为xml文件,重启后读取

窗体中我勾选选中了一个RadioButton可是为了遍历窗体中所有内容,我要重启窗体,但是重启后RadioButton又回到了默认选中的那一项,现在我想在重启前保存到xml文件,然后在重启后读取该文件。具体该怎样操作,求指导

直接绑定下配置就可以了。
https://blog.csdn.net/weixin_43680751/article/details/122109163

将RadioButton属性保存为XML文件的步骤如下:

  1. 在窗体中,找到要保存RadioButton属性的RadioButton对象,并获取其属性值。
RadioButton selectedRadioButton = this.FindControl("RadioButton1") as RadioButton;
string selectedRadioButtonValue = selectedRadioButton.Value;
  1. 创建一个XML文件,并将其命名为与RadioButton属性值相同的文件名。

  2. 使用Visual Studio的调试工具,打开保存的XML文件,并查找与RadioButton属性值相同的节点。

using System.IO;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // 获取RadioButton对象属性值
        string selectedRadioButtonValue = this.FindControl("RadioButton1") as RadioButton?.Value;
        // 创建XML文件
        string xmlFile = "RadioButtons.xml";
        string xmlText = string.Join("", this.FindAllNodes(xmlFile));
        // 保存XML文件
        File.WriteAllText(xmlFile, xmlText);
    }
}
  1. 重启窗体。

  2. 在重启后,使用Visual Studio的调试工具,打开保存的XML文件,并查找与RadioButton属性值相同的节点。

using System.IO;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // 保存RadioButton属性值
        string selectedRadioButtonValue = this.FindControl("RadioButton1") as RadioButton?.Value;
        string xmlFile = "RadioButtons.xml";
        string xmlText = string.Join("", this.FindAllNodes(xmlFile));
        File.WriteAllText(xmlFile,xmlText);

        // 重启窗体
        this.Close();
        this.Window.Title = "重启后";
        Windows.ApplicationModel.Core.Application.Exit();
    }
}

以上就是将RadioButton属性保存为XML文件,并在重启前读取该文件的详细步骤。