DataTemplate 中的单选按钮
Radio Button in 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>
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
属性的对象
我找到了另一种方法,因为我将源 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>
这对我有用,但我必须将所有 4 个单选按钮分组到 1 个 WrapPanel 中,因为如果我将它们分成 2 个面板,它们将有自己的组名。但是 1 个 WrapPanel 中的 4 组让它们看起来很糟糕 (1),因为我想要类似 (2) 的东西,而不是 (1)
(2)
(问题)
(甲) (乙)
(C) (D)
不是
(1)
(问题)
(A) (B) (C)
(D)
无论如何,谢谢你的帮助:)
我制作了一个会自动生成一些问题的数据模板(从我的数据视图加载)。每个问题都有四个单选按钮,我将所有四个按钮分组到 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>
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
属性的对象
我找到了另一种方法,因为我将源 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>
这对我有用,但我必须将所有 4 个单选按钮分组到 1 个 WrapPanel 中,因为如果我将它们分成 2 个面板,它们将有自己的组名。但是 1 个 WrapPanel 中的 4 组让它们看起来很糟糕 (1),因为我想要类似 (2) 的东西,而不是 (1)
(2) (问题)
(甲) (乙)
(C) (D)
不是
(1)
(问题)
(A) (B) (C)
(D)
无论如何,谢谢你的帮助:)