为什么带有单选按钮的自编程用户控件不同步?

Why are self-programmed usercontrols with RadioButtons not synchronized?

我已经为 WPF 应用程序编写了一些用户控件。 其中之一是实现 RadioButton 的 UserControl。 这是 UserControl 的 XAML:

<UserControl x:Class="UsercontrolExample.UserControls.ControlRadioButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UsercontrolExample.UserControls"
             mc:Ignorable="d"
             d:DesignHeight="25"
             d:DesignWidth="150">
    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border x:Name="border"
                Style="{StaticResource borderbasestyle}">
            <RadioButton x:Name="radiobutton"
                         Content="RadioButton"
                         Margin="2"/>
        </Border>
    </Grid>
</UserControl>

在 cs 文件中我编写了一个名为 "ControlIsChecked" 的 属性 用于调整 RadioButton 的 IsChecked 属性 并且在构造函数中我将其设置为 False,这应该是单选按钮标准。

/// <summary>
/// Gets or sets if the checkbox is checked.
/// </summary>
public bool? ControlIsChecked
{
    get { return radiobutton.IsChecked; }
    set { radiobutton.IsChecked = value; }
}

在用于测试新用户控件的 WPF-window 上,我将两个 ControlRadioButton 放在 Canvas 上,它位于 GroupBox 上。

<GroupBox x:Name="groupbox1" Header="Selection" Canvas.Left="24" Canvas.Top="152" Height="98" Width="157">
    <Canvas x:Name="canvasgroupbox1">
        <Controls:ControlRadioButton x:Name="radiobutton1"
                                     ControlContent="RadioButton-1"
                                     ControlFontSize="12"
                                     ControlFontIsBold="True"
                                     ControlFontStyleIsItalic="True"
                                     ControlIsChecked="True"
                                     Canvas.Left="24" Canvas.Top="10"/>
        <Controls:ControlRadioButton x:Name="radiobutton2"
                                     ControlContent="RadioButton-2"
                                     Canvas.Left="24" Canvas.Top="45"/>
    </Canvas>
</GroupBox>

不幸的是,我的 RadioButtons 在运行时没有同步。 通常,当检查第一个radiobutton并检查第二个radiobutton时,第一个应该不选中。 但在我的情况下,两者都被检查了。

如何同步单选按钮?

提前致谢!

非常感谢您的回答和启发。 与此同时,我找到了另一种临时方式。 我们正在编写一个动态 WPF 应用程序,该应用程序 Windows 应由保存在数据库中的元数据生成。 顺便说一下,像我们自己的 RadioButton 这样的控件总是放在 Canvas 上。 所以我试图找出那个祖先的名字并将其绑定到组名:

<RadioButton x:Name="radiobutton"
             GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}}, Path=Name}"
             Margin="2">
    <TextBlock x:Name="textblock"
               Text="RadioButton"
               TextWrapping="Wrap"></TextBlock>
</RadioButton>

到时候我会尝试一下ASh的提议