当我选择低选项时,也会选中高选项(因此检查了2个选项),如下图所示:
但是运行应用程序时,只选择了低选项。我想在选择低选项时,只检查低选项。
XAML:
<StackPanel x:Name="highStack" Grid.Row="0" Orientation="Vertical">
<RadioButton x:Name="highBtn" Margin="0,10,0,0" Content="High" FontSize="18" IsChecked="True" HorizontalAlignment="Left"/>
<TextBlock x:Name="highSize" Margin="30,5,0,0" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel x:Name="medStack" Grid.Row="1" Orientation="Vertical">
<RadioButton x:Name="medBtn" Margin="0,15,0,0" Content="Medium" FontSize="18" IsChecked="False" HorizontalAlignment="Left"/>
<TextBlock x:Name="medSize" Margin="30,5,0,0" FontSize="15" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel x:Name="lowStack" Grid.Row="2" Orientation="Vertical">
<RadioButton x:Name="lowBtn" Margin="0,15,0,0" Content="Low" FontSize="18" IsChecked="False" HorizontalAlignment="Left"/>
<TextBlock x:Name="lowSize" Margin="30,5,0,0" FontSize="15" HorizontalAlignment="Left"/>
</StackPanel>
如何处理?
答案 0 :(得分:2)
您需要设置GroupName
属性:
<StackPanel x:Name="highStack" Grid.Row="0" Orientation="Vertical">
<RadioButton x:Name="highBtn" GroupName = "MyGroup" Margin="0,10,0,0" Content="High" FontSize="18" IsChecked="True" HorizontalAlignment="Left"/>
<TextBlock x:Name="highSize" Margin="30,5,0,0" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel x:Name="medStack" Grid.Row="1" Orientation="Vertical">
<RadioButton x:Name="medBtn" GroupName = "MyGroup" Margin="0,15,0,0" Content="Medium" FontSize="18" IsChecked="False" HorizontalAlignment="Left"/>
<TextBlock x:Name="medSize" Margin="30,5,0,0" FontSize="15" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel x:Name="lowStack" Grid.Row="2" Orientation="Vertical">
<RadioButton x:Name="lowBtn" GroupName = "MyGroup" Margin="0,15,0,0" Content="Low" FontSize="18" IsChecked="False" HorizontalAlignment="Left"/>
<TextBlock x:Name="lowSize" Margin="30,5,0,0" FontSize="15" HorizontalAlignment="Left"/>
</StackPanel>
单选按钮可以隐式分组(例如在同一StackPanel
内),或明确分组(如上所述)。请参阅Microsoft Guidelines on RadioButtons in UWP。