wpf用户控件RadioButton预选

发布于 2024-08-13 18:40:59 字数 1004 浏览 2 评论 0原文

我有一个非常简单的用户控件(visifire 图表+日期选择器和一些用于控制过滤等的单选按钮):

<GroupBox Header="Scale" Width="159" HorizontalAlignment="Left" Margin="10,47,0,0"
          Height="38" VerticalAlignment="Top" Name="scalingBox">
    <Canvas Height="16">
        <RadioButton Name="scaleDays" Content="Days" GroupName="g2" Width="47"
                     IsChecked="True" Checked="scale_Change"/>
        <RadioButton Name="scaleHours" Content="Hours" GroupName="g2" Canvas.Left="60"
                     Width="59" Checked="scale_Change"/>
    </Canvas>
</GroupBox>

首先,在第一个按钮上将 IsChecked 设置为 true 在显示控件时不会执行任何操作。

在寻找解决方法时,我公开了一些公共方法来从父窗口检查我想要的按钮,但是当我有多个 MyControl 调用这些方法时,仅检查方法最后一个实例上的按钮叫。像这样:

myControl1.SetDefaultScale();
myControl2.SetDefaultScale();

仅检查 myControl2 上的按钮,而不检查 myControl1 上组中的所有按钮。

有什么想法可能导致这种情况吗?


是的,这是所有控件之间共享的组名称......谢谢阿兰

I got a very simple user control (visifire charts + datepickers and some radiobuttons to control filtering etc):

<GroupBox Header="Scale" Width="159" HorizontalAlignment="Left" Margin="10,47,0,0"
          Height="38" VerticalAlignment="Top" Name="scalingBox">
    <Canvas Height="16">
        <RadioButton Name="scaleDays" Content="Days" GroupName="g2" Width="47"
                     IsChecked="True" Checked="scale_Change"/>
        <RadioButton Name="scaleHours" Content="Hours" GroupName="g2" Canvas.Left="60"
                     Width="59" Checked="scale_Change"/>
    </Canvas>
</GroupBox>

First bit is that setting IsChecked to true on the first button doesn't do anything when the control is displayed.

Searching for a workaround I exposed some public methods to check the buttons I want from the parent window, but when I have more than one MyControl calling these methods only checks the buttons on one instance where the methods was last called. like this:

myControl1.SetDefaultScale();
myControl2.SetDefaultScale();

only checks the buttons on myControl2, leaving all the buttons in the group unchecked on myControl1.

Any ideas what may be causing this?


And yes, it was the group name that got shared among all the controls... Thanks Aran

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2024-08-20 18:40:59

我得到了你的代码,将其放入新项目和主窗口中。第一个按钮被选中。

RadioButtons 有一些错误,首先,如果您单击它们,它们会丢失绑定,请参阅 此处。我有过您所描述的在表单之间共享组名称的行为,如 其他一些人。我解决这些问题的方法是单独绑定每个单选按钮并自己处理选择逻辑。 (如果选择了其中一个,则将“组”中的其他属性绑定到的属性设置为 false)

我希望它在下一版本中得到修复。但与此同时,我单独绑定每个单选按钮并且不使用组名称。

i got your code, put it in a new project and on the main window. and the first button came up checked.

RadioButtons have some bugs, first they lose their bindings if you click on them see here. I have had the behaviour you describe where group names are shared between forms, as have some other people. how i get around these issues is to bind each radio button seperately and handle the selection logic myself. (if one is selected set the properties that the others in the 'group' are binding to to be false)

im hoping it gets fixed in the next release. but for the meantime i bind every radio button seperately and dont use group names.

っ〆星空下的拥抱 2024-08-20 18:40:59

表单上只能勾选一个单选按钮的原因与GroupName的范围有关。

当我尝试使用两个带有单选按钮且未指定 GroupName 的用户控件实例时,单选按钮效果的范围仅限于用户控件内的组框。

当为用户控件中的单选按钮指定相同的 GroupName 时,这意味着窗口中的所有单选按钮都具有相同的 GroupName 并且包含在同一组中。整个窗口中的组中的按钮一次只能被选中。

答案是从单选按钮中删除 GroupName 属性,让 GroupBox 的自然分组生效。

The cause of why only one radio button may be checked on the form is related to the scope of GroupName.

When I tried with two instances of user controls with radio buttons and no GroupName specified, the scope of the radio button effect was limited to the group box inside the user control.

When the same GroupName was specified for the radio buttons in the user control that meant that all of the radio buttons in the window had the same GroupName and are included in the same group. Only one button in the group in the whole window may be checked at one time.

The answer is to remove the GroupName property from the radio buttons and let the natural grouping of the GroupBox take effect.

阳光下的泡沫是彩色的 2024-08-20 18:40:59

使用相同的用户控件的多个选项卡也有类似的问题。删除了 GroupName,它按预期工作。 Doug 指出,它们在所有创建的对象之间共享(广泛范围)。通过手动设置检查值来实现此工作。

I have a similar issues with multiple tabs using the same usercontrol. Removed the GroupName and it worked as expected. Doug pointed out that they are shared across all of the created object (wide scope). Got this working by manually set the checked value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文