2014-01-14 41 views
1

我在MVVM/prism应用程序中绑定到数据库数据的itemscontrols和datatemplates中有多个比例按钮。每组单选按钮都会相应地分组,以便它们是分离组。WPF - 允许在单选按钮组中多选的错误

我遇到的问题(并违背单选按钮的惯例)是您可以在组中选择多个选项。并非所有选项都允许多个选择。有些人会像其他人一样行事。在通过探听进行检查时,所有单选按钮都属于同一组,但多个按钮对IsChecked报告正确。

任何想法?

感谢

编辑 - 代码

XAML

<StackPanel Grid.Column="0" Margin="10,0,0,10"> 
             <TextBlock Margin="5,5,0,5" 
                FontSize="16" 
                FontWeight="Bold" 
                Foreground="{Binding  Path=ThemeBackground}" 
                Text="From" /> 
             <ItemsControl ItemsSource="{Binding Path=InternetItems}"> 
              <ItemsControl.ItemTemplate> 
               <DataTemplate> 
                <RadioButton Margin="5" 
                   Content="{Binding Path=Title}" 
                   GroupName="InternetFrom" 
                   IsChecked="{Binding Path=IsSelected}" 
                   IsEnabled="{Binding Path=IsEnabled}" /> 
               </DataTemplate> 
              </ItemsControl.ItemTemplate> 
             </ItemsControl> 
            </StackPanel> 

视图模型

public ObservableCollection<Item> InternetItems 
    { 
     get 
     { 
      return 
       new ObservableCollection<Item>(
        _items.Where(x => x.Category == Category.InternetFrom).OrderBy(x => x.DisplayOrder)); 
     } 

    } 

编辑 -

问题解决。后面的代码是启动一个新的可观察集合,每次选择单选按钮导致多个数据环境,而不管单选按钮的组名是否相同

+3

发布相关代码和XAML。 –

回答

2

您在做错某事,RadioButton s在同一组中是互斥的。如果使用GroupName属性将两个或多个RadioButton分配给一个组,则只能从该组中选择一个RadioButton

RadioButton.GroupName Property

获取或设置指定哪些单选按钮控件是 互斥的名称。

用户可以在每个组中选择一个RadioButton。

你真的认为你发现了.NET框架中的这样一个基本的错误,没有人报告过,并且还没有被修复吗?

+0

我认为这应该是在我之前很久就会注意到的事情。但是我在同一页面上有多个ItemControls(不同的组名),我读过很多问题,人们只能在同一个视图中选择多个组。我想知道是否有可能设置一种风格,并允许这样做? 对不起没有代码。我在家里工作...... – user3195554

+0

@ user3195554无论你的问题是什么,我敢肯定这是'你的'代码问题,而不是WPF。我已经实现了基于RadioButton的UI的TON,并且从不面临任何问题。 –

+0

我同意这是我的代码。我从来没有说过其他的明智,... – user3195554

3

RadioButton如果控件共享相同的容器(例如,任何派生自PanelContentControl的控件),它们是相互独立的。在你的情况下,你的ItemsControl生成的每个项目是一个单独的容器,因此按钮不会自动相互排斥。

例如: 如果您ItemsControl是设置这样的按钮是互斥的:

<ItemsControl> 
    <RadioButton Content="1" /> 
    <RadioButton Content="2" /> 
    <RadioButton Content="3" /> 
    <RadioButton Content="4" /> 
</ItemsControl> 

但是,这并不:

<ItemsControl> 
    <Grid> 
     <RadioButton Content="1" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="2" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="3" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="4" /> 
    </Grid> 
</ItemsControl> 

正如Dean说,分配相同的GroupName财产将解决你的问题。

<ItemsControl> 
    <Grid> 
     <RadioButton Content="1" 
         GroupName="Group1" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="2" 
         GroupName="Group1" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="3" 
         GroupName="Group1" /> 
    </Grid> 
    <Grid> 
     <RadioButton Content="4" 
         GroupName="Group1" /> 
    </Grid> 
</ItemsControl> 

编辑

如果您有多个ItemsControl S,你可以简单地在每个ItemsControl设置不同GroupNameRadioButton秒。在这种情况下,范围内的默认样式派上用场:

<StackPanel> 
    <ItemsControl> 
     <ItemsControl.Resources> 
      <Style TargetType="{x:Type RadioButton}"> 
       <Setter Property="GroupName" 
         Value="Group1" /> 
      </Style> 
     </ItemsControl.Resources> 
     <Grid> 
      <RadioButton Content="1" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="2" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="3" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="4" /> 
     </Grid> 
    </ItemsControl> 
    <ItemsControl> 
     <ItemsControl.Resources> 
      <Style TargetType="{x:Type RadioButton}"> 
       <Setter Property="GroupName" 
         Value="Group2" /> 
      </Style> 
     </ItemsControl.Resources> 
     <Grid> 
      <RadioButton Content="1" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="2" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="3" /> 
     </Grid> 
     <Grid> 
      <RadioButton Content="4" /> 
     </Grid> 
    </ItemsControl> 
</StackPanel>