单独选项卡上的 WPF 用户控件:为什么单选按钮组名在选项卡之间共享?

发布于 2024-09-02 13:34:48 字数 1637 浏览 1 评论 0原文

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

看来单选按钮组名称在选项卡之间共享。到底是怎么回事?

简单示例:

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

<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>

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

<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,尽管用户控件将其定义为始终在启动时检查。

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

看起来在鼠标控制下一切都表现良好(即选项卡是独立的)。

最后,用户控件似乎确实是单独的实例化。例如,我已经尝试过使用用户控件上的滑块进行此操作,并且它们确实在选项卡之间独立运行。正如他们应该的那样。

感谢任何人对此的帮助。我进行了广泛的搜索,但没有结果。当然我不是唯一遇到这个问题的人。我用的是VS2008。

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?

Simple example:

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>

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.

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.

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

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

发布评论

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

评论(4

葵雨 2024-09-09 13:34:49

我遇到过这样的情况:设计不允许将无线电分成单独的堆栈。因此,我所做的就是从 xaml 中删除 GroupName 并强制从模型中进行分组。这个想法来自这个链接,例如

private bool _useGaugeOpen;
public bool UseGaugeOpen
{
    get { return _useGaugeOpen; }
    set
    {
        _useGaugeOpen = value;
        _useATGOpen = !value;
        RaisePropertyChanged("UseATGOpen");            
    }
}
private bool _useATGOpen;
public bool UseATGOpen
{
    get { return _useATGOpen; }
    set
    {
         _useATGOpen = value;
         _useGaugeOpen = !value;
         RaisePropertyChanged("UseGaugeOpen");
    }
 }

I had a situation where the design did not allow separation of the radios into separate stacks. So, what I did was remove GroupName from the xaml and enforce the grouping from the model. This idea came from this link, e.g.

private bool _useGaugeOpen;
public bool UseGaugeOpen
{
    get { return _useGaugeOpen; }
    set
    {
        _useGaugeOpen = value;
        _useATGOpen = !value;
        RaisePropertyChanged("UseATGOpen");            
    }
}
private bool _useATGOpen;
public bool UseATGOpen
{
    get { return _useATGOpen; }
    set
    {
         _useATGOpen = value;
         _useGaugeOpen = !value;
         RaisePropertyChanged("UseGaugeOpen");
    }
 }
栀梦 2024-09-09 13:34:49

我通过通过 /Common/GroupName 下的 XAML 属性框一致地命名每组单选按钮的组来解决我的问题。现在他们只与同一组中的其他人互动。

I got my problem fixed by just naming the groups of each set of radio buttons consistently via the XAML properties box under: /Common/GroupName. Now they only interact with the others in the same group.

樱娆 2024-09-09 13:34:48

如果没有设置 GroupName,它就可以工作。这并不是绝对必要的,因为一个容器中的单选按钮无论如何都会自动分组。例如:

<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>

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>
森林很绿却致人迷途 2024-09-09 13:34:48

此行为是设计使然:

GroupName 的全部意义在于允许您定义单选按钮
作为一个群体而不必被同一个父级包含
面板。

请阅读本文了解详细说明。

简而言之,对于两种不同的情况有两种解决方案:

  • 如果您将所有逻辑上相关的单选按钮放在一个容器中,则不要指定GroupName。在这种情况下,单选按钮将自动形成一个独立于任何其他单选按钮组的组。

  • 如果出于布局目的将逻辑相关的单选按钮拆分到多个面板上,则 阅读文章,了解该怎么做的详细说明。

This behaviour is by design:

the whole point of GroupName is to allow you to define radio buttons
that act as a group without having to be contained by the same parent
panel.

Please read this article for detailed explanation.

To speak briefly, there are two solutions for two different cases:

  • If you have all logically related radio buttons in one container, then don't specify a GroupName. In this case radio buttons will automatically form a group that's independent of any other radio button groups.

  • If you split logically related radio buttons across multiple panels for layout purposes, then read the article for a lengthy explanation what to do.

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