怎样在后台想布局容器添加radiobutton控件并让其对齐

大家上午好,我有两个问题,第一个问题,在一个界面上,我有一个按钮,点开是一个窗口,在这个窗口中我将这个窗口的Grid分成两行,在第一行我在后台.cs中添加
添加相同数量Textblock和RadioButton控件,先添加一个Textblock在添加一个RadioButton,如下图

img


先添加Textblock再添加RadioButtonTextblock相当于我想表示它后面添加radiobutton名字, 添加完后效果如下图

img

怎样有办法能让我添加进来的每一组Textblock和radiobutton在一起并且所有的对整齐,比如让它有几行几列,一行一个多少个,一列有多少个,
而不是有的行八个,有的九个,对整齐,不要像图上一样,有的textblock跟在它后面的radiobutton都在下一行了,看着很乱。
第二个问题就是当我点关掉窗口再打开时,窗口中所有的Radiobutton又都是不选中的状态,我想就是当我点击确认按钮时保存我当时所做的操作,比如当我打开窗口选中了一个radiobuton,点击确认按钮后,当我再打开时,那个radiobutton是选中状态,而不是像我现在,打开又全没选中。

各位大佬,我想radiobuutton状态只能存在文件里,因为存在内存里我会重启程序保存不了

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
第一个问题:
可以使用WPF中的Grid来实现整齐排列。它可以让控件像表格一样排列,并且可以控制行列数、大小等属性。示例代码如下:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
 
    <TextBlock Grid.Row="0" Grid.Column="0" Text="Text1" />
    <RadioButton Grid.Row="0" Grid.Column="1" />
 
    <TextBlock Grid.Row="1" Grid.Column="0" Text="Text2" />
    <RadioButton Grid.Row="1" Grid.Column="1" />
 
    <TextBlock Grid.Row="2" Grid.Column="0" Text="Text3" />
    <RadioButton Grid.Row="2" Grid.Column="1" />
 
    <TextBlock Grid.Row="3" Grid.Column="0" Text="Text4" />
    <RadioButton Grid.Row="3" Grid.Column="1" />
</Grid>

这个Grid有两列,第一列宽度自适应,第二列占据剩余宽度。有四行,每行包含一个TextBlock和一个RadioButton。Grid会根据属性将其排列。

第二个问题:
可以在窗口关闭前保存选中状态,在窗口打开时再遍历控件设置选中状态。示例代码如下:

public partial class MainWindow : Window
{
    private Dictionary<RadioButton, bool> _radioButtonStates = new Dictionary<RadioButton, bool>();
 
    private void SaveRadioButtonStates()
    {
        foreach (var radioButton in FindVisualChildren<RadioButton>(this))
        {
            _radioButtonStates[radioButton] = radioButton.IsChecked ?? false;
        }
    }
 
    private void RestoreRadioButtonStates()
    {
        foreach (var radioButton in FindVisualChildren<RadioButton>(this))
        {
            if (_radioButtonStates.TryGetValue(radioButton, out bool isChecked))
            {
                radioButton.IsChecked = isChecked;
            }
        }
    }
 
    private IEnumerable<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child != null && child is T t)
            {
                yield return t;
            }
 
            foreach (T found in FindVisualChildren<T>(child))
            {
                yield return found;
            }
        }
    }
 
    private void Window_Closed(object sender, EventArgs e)
    {
        SaveRadioButtonStates();
    }
 
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        RestoreRadioButtonStates();
    }
 
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window2 window = new Window2();
        window.ShowDialog();
    }
}

这里定义了一个字典,用于保存RadioButton的选中状态。在窗口关闭前遍历所有RadioButton,保存它们的状态。在窗口打开时遍历所有RadioButton,将其选中状态设置为保存的状态。注意,这里的FindVisualChildren方法用于遍历窗口中的所有控件,详细解释可以看这篇文章:https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type/636408#636408

希望能对您有所帮助。
如果我的回答解决了您的问