我制作了一个自动生成一些问题的DataTemplate(从我的数据视图加载)。每个问题都有四个单选按钮,我将所有四个按钮分组到GroupName上。
我的问题是 - 如果我有2或3个问题,每当我在一个问题上检查单选按钮时,上面问题的单选按钮将被取消选中。有人有解决方案吗?
这是我的XAML:
<ItemsControl Name="itmCntrl" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" ></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="stk">
<StackPanel Margin="5,2,0,0">
<WrapPanel Margin="10,10,10,5" Height="Auto">
<TextBlock TextWrapping="Wrap" x:Name="tbC" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Text="Question : "/>
<TextBlock TextWrapping="Wrap" Text="{Binding NOIDUNG}" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
</WrapPanel>
<WrapPanel Margin="10,5,10,5" Height="Auto">
<RadioButton x:Name="rdoA" GroupName="DA"/>
<TextBlock TextWrapping="Wrap" Name="A" Text="{Binding A}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoB" GroupName="DA" Margin="10,0,0,0"/>
<TextBlock TextWrapping="Wrap" Name="B" Text="{Binding B}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
</WrapPanel>
<WrapPanel Margin="10,5,10,5" Height="Auto">
<RadioButton x:Name="rdoC" GroupName="DA"/>
<TextBlock TextWrapping="Wrap" Name="C" Text="{Binding C}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoD" GroupName="DA" Margin="10,0,0,0"/>
<TextBlock TextWrapping="Wrap" Name="D" Text="{Binding D}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
</WrapPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
答案 0 :(得分:3)
RadioButton.GroupName
Property是DependencyProperty
,这意味着您可以为每个组绑定唯一值的数据:
<DataTemplate>
<StackPanel Name="stk">
<StackPanel Margin="5,2,0,0">
<WrapPanel Margin="10,10,10,5" Height="Auto">
<TextBlock TextWrapping="Wrap" x:Name="tbC" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Text="Question : "/>
<TextBlock TextWrapping="Wrap" Text="{Binding NOIDUNG}" FontSize="18" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
</WrapPanel>
<WrapPanel Margin="10,5,10,5" Height="Auto">
<RadioButton x:Name="rdoA" GroupName="{Binding GroupName}"/>
<TextBlock TextWrapping="Wrap" Name="A" Text="{Binding A}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoB" GroupName="{Binding GroupName}" Margin="10,0,0,0"/>
<TextBlock TextWrapping="Wrap" Name="B" Text="{Binding B}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
</WrapPanel>
<WrapPanel Margin="10,5,10,5" Height="Auto">
<RadioButton x:Name="rdoC" GroupName="{Binding GroupName}"/>
<TextBlock TextWrapping="Wrap" Name="C" Text="{Binding C}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoD" GroupName="{Binding GroupName}" Margin="10,0,0,0"/>
<TextBlock TextWrapping="Wrap" Name="D" Text="{Binding D}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
</WrapPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
要使此示例有效,您只需要将GroupName
属性添加到您在DataTemplate
中绑定数据的视图模型中,即。包含A
,B
,C
和D
属性的对象。
答案 1 :(得分:0)
我找到了另一种方法,因为我将源Obj绑定到我的Itemcontrol,所以我使用这个方法:
<WrapPanel Margin="10,5,10,5" Height="Auto">
<RadioButton x:Name="rdoA" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=A}"/>
<TextBlock TextWrapping="Wrap" Name="A" Text="{Binding A}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoB" Margin="10,0,0,0" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=B}"/>
<TextBlock TextWrapping="Wrap" Name="B" Text="{Binding B}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<TextBlock Text="
"/>
<RadioButton x:Name="rdoC" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=C}"/>
<TextBlock TextWrapping="Wrap" Name="C" Text="{Binding C}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
<RadioButton x:Name="rdoD" GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=D}" Margin="10,0,0,0"/>
<TextBlock TextWrapping="Wrap" Name="D" Text="{Binding D}" FontSize="15" FontFamily="Times New Roman" FontWeight="Bold" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="Black" Margin="3,0,0,0"/>
</WrapPanel>
这适用于我,但我必须将1个WrapPanel上的所有4个单选按钮分组,因为如果我将它们分成两个面板,它们将拥有自己的组名。但是他们在1个WrapPanel中的第4组让他们看起来很糟糕(1),因为我想要一些像(2),而不是(1)
(2) (问题)
(A)(B)
(C)(D)
不是
(1)
(问题)
(A)(B)(C)
(d)
无论如何,请帮忙:)