是否可以在第二次点击时将radioButton的状态设置为IsChecked = false?

时间:2011-12-06 07:35:04

标签: silverlight xaml

我在一组中有4个单选按钮。 单击单选按钮后,状态将更改为IsChecked = true。 我想在单击选中的单选按钮时将单选按钮的状态更改为IsChecked = false。 这在XAML中是否可行?

2 个答案:

答案 0 :(得分:0)

在radiobutton的Checked和Unchecked事件中执行此操作

    public bool Ischecked { get; set; }
    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        Ischecked = true;
    }

    private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
    {
        Ischecked = false;
    }

答案 1 :(得分:0)

您需要跟踪组中四个单选按钮的已检查状态。每次单击任何单选按钮时,您都需要将状态更新为最新状态。以下是您可以尝试的代码。

XAML代码......

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<StackPanel x:Name="LayoutRoot" Background="White">
    <RadioButton GroupName="LotOfFour" Tag="0" Click="RadioButton_Click" Checked="RadioButton_Checked" Unchecked="RadioButton_Unchecked"/>
    <RadioButton GroupName="LotOfFour" Tag="1" Click="RadioButton_Click" Checked="RadioButton_Checked" Unchecked="RadioButton_Unchecked"/>
    <RadioButton GroupName="LotOfFour" Tag="2" Click="RadioButton_Click" Checked="RadioButton_Checked" Unchecked="RadioButton_Unchecked"/>
    <RadioButton GroupName="LotOfFour" Tag="3" Click="RadioButton_Click" Checked="RadioButton_Checked" Unchecked="RadioButton_Unchecked"/>
</StackPanel>
</UserControl>

代码背后......

public partial class MainPage : UserControl
{
    private Boolean[] _ischecked; 
    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        _ischecked = new Boolean[] { false, false, false, false };
    }

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Checked");
    }

    private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Unchecked");
    }

    private void RadioButton_Click(object sender, RoutedEventArgs e)
    {
        RadioButton rbSource = sender as RadioButton;
        if ((rbSource != null) && (rbSource.IsChecked == true))
        {
            try
            {
                Int32 index = Int32.Parse(rbSource.Tag.ToString());
                Boolean isSourceChecked = _ischecked[index];
                // Now reset all to false, source will be set accordingly in ifelse block.
                _ischecked[0] = false;
                _ischecked[1] = false;
                _ischecked[2] = false;
                _ischecked[3] = false;
                if (isSourceChecked == true)
                {
                    _ischecked[index] = false;
                    rbSource.IsChecked = false;
                }
                else
                {
                    _ischecked[index] = true;
                }
            }
            catch (Exception) { }
        }
    }
}