单独选项卡上的 WPF 用户控件:为什么选项卡之间共享单选按钮组名? [英] WPF usercontrols on separate tabs: why is radiobutton groupname shared between tabs?

查看:60
本文介绍了单独选项卡上的 WPF 用户控件:为什么选项卡之间共享单选按钮组名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WPF 选项卡控件来呈现用户控件的单独重复实例.即用于 Item1 设置的 Tab1,用于 Item2 设置的 Tab2,依此类推.

I am using a WPF tab control to present separate repeated instances of a user control. i.e. Tab1 for Item1 settings, Tab2 for Item2 settings, and so on.

单选按钮组名称似乎在选项卡之间共享.这是怎么回事?

It appears that the radio button group names are being shared between tabs. What is going on?

简单例子:

一个窗口包含选项卡.每个选项卡都包含一个用户控件.

A window contains tabs. Each tab contains a user control.

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lib="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
    <TabControl Margin="0,0,0,100" Name="tabControl1">
        <TabItem Header="tabItem1" Name="tabItem1">
            <lib:UserControl1 x:Name="userControlInTab1" />
        </TabItem>
        <TabItem Header="tabItem2" Name="tabItem2">
            <lib:UserControl1 x:Name="userControlInTab2" />
        </TabItem>
    </TabControl>
</Grid>

用户控件只是一组中的两个单选按钮:

The user control is simply two radiobuttons in a group:

<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="50" Width="100">
<StackPanel>
    <RadioButton GroupName="Group1" Name="radiobutton1" Content="option1" IsChecked="True" />
    <RadioButton GroupName="Group1" Name="radiobutton2" Content="option2" />
</StackPanel>

如果您运行此应用程序,您将看到仅选中第二个选项卡中的 radiobutton1,尽管用户控件将其定义为始终在启动时选中.

If you run this app, you will see that only the radiobutton1 in the second tab is checked, despite the usercontrol defining it to always be checked at startup.

此外,将单选按钮设置为已签入代码后面似乎会取消选中其他选项卡中的所有单选按钮!

Further, setting a radiobutton as checked in code behind seems to uncheck all the radiobuttons in other tabs!

似乎在鼠标控制下一切正常(即标签是独立的).

It seems like things behave fine under mouse control (i.e. tabs are independent).

最后,用户控件似乎是单独的实例化.例如,我曾在用户控件上使用滑块进行过尝试,并且它们在选项卡之间的行为是独立的.他们应该这样做.

Lastly, the usercontrols do seem to be separate instantiations. I have tried this with sliders on user controls, for example, and they do behave independently across tabs. As they should.

感谢任何人对此的帮助.我已经广泛搜索无济于事.当然,我不是唯一遇到这个问题的人.我使用的是 VS2008.

Thanks for anyone's help with this. I have searched widely to no avail. Surely I'm not the only person who has had this issue. I'm using VS2008.

推荐答案

如果没有设置 GroupName,它也能工作.这不是绝对必要的,因为一个容器中的 RadioButtons 无论如何都会自动分组.例如:

Without the GroupName set it works. It is not strictly necessary since the RadioButtons in one container are automatically grouped anyway. For example:

<UserControl x:Class="WpfApplication1.UserControl1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="50" Width="100">
    <StackPanel>
        <StackPanel>
            <RadioButton  Name="radiobutton1" Content="option1" IsChecked="True" />
            <RadioButton  Name="radiobutton2" Content="option2" />
        </StackPanel>
        <StackPanel>
            <RadioButton  Name="radiobutton3" Content="option3" IsChecked="True" />
            <RadioButton  Name="radiobutton4" Content="option4" />
        </StackPanel>
    </StackPanel>
</UserControl>

这篇关于单独选项卡上的 WPF 用户控件:为什么选项卡之间共享单选按钮组名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆