WPF 是否有保存单选按钮内容,然后通过事件处理程序发送保存内容的方法



我想知道WPF是否可以保存单选按钮内容并使用事件处理程序将内容发送到文本文件。我是否需要在事件处理程序中执行所有这些操作,或者我可以调用某个类?

    public void method(){
string s = "Test1&Test2&Test3&Test4";
string[] s = Spliting.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
List<RadioButton> radioButtonList = new List<RadioButton>();
        radioButtonList.Add(radioButton);
        radioButtonList.Add(radioButton1);
    for (int i = 0; i < radioButtonList.Count; i++)
{
//some code that insert string into the radio button (done)
//when i use streamwriter inside here, it give me this in the text file
//System.Windows.Controls.RadioButton Content IsChecked:False <- this can be turn to true if i put variable.Checked = true
}
private void radioButton_Checked(object sender, RoutedEventArgs e)
    {
        if (radioButton.IsChecked == true)
        {
            StreamWriter streamwriters = new StreamWriter ("Sent.txt", false);
            streamwriters.Close();
        }
        else if (radioButton1.IsChecked == true)
        {
            StreamWriter streamwriters = new StreamWriter("Sent.txt", false);
            streamwriters.Close();
        }

或者 WPF 有一个可以自动将内容保存到变量中的方法。我错过了什么吗?

我希望单选按钮

检查单选按钮并将内容发送到文本文件中。

如果您只想将使用单选按钮显示的文本保存到文件中,则:

private void radioButton1_Checked(object sender, EventArgs e)
{
    string radioButtonContent = ((RadioButton)sender).Text;
    string savePath = @"C:sent.txt"; // or wherever
    File.WriteAllText(savePath, radioButtonContent);
}

最新更新