如何使用复选框获取真假值?

时间:2018-12-01 04:00:44

标签: c# wpf visual-studio oop

主窗口中有一个名为String CustomerId的变量。

String CustomerId = customer_id_textbox.Text

类似地,我在主窗口中有一个名为bool customer_is_adult的变量。

我试图找出如何使用复选框将值分配给customer_is_adult的方法。 如果选中了customer_checkbox,则此变量的值应为true,如果未选中,则其值应为false。

4 个答案:

答案 0 :(得分:0)

在Checkbox对象上有一个Checked属性,您可以在此处使用。

bool customer_is_adult = customer_checkbox.Checked;

答案 1 :(得分:0)

您可以使用CheckBox CheckedChanged事件:

private void customer_checkbox_CheckedChanged(Object sender, EventArgs e) 
{
    customer_is_adult = customer_checkbox.Checked;
}

请看看CheckBox.CheckedChanged Event

答案 2 :(得分:0)

private void CustomerCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            bool customer_is_adult = true;
        }
        private void CustomerCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            bool customer_is_adult = false;
        }

答案 3 :(得分:0)

Please Try this Code



<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <CheckBox
            Content="CheckBox"
            HorizontalAlignment="Left"
            Margin="10,10,0,0"
            VerticalAlignment="Top"
            Checked="CheckBox_Checked"
            Unchecked="CheckBox_Unchecked"/>
    </Grid>
</Window>




public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Handle(sender as CheckBox);
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            Handle(sender as CheckBox);
        }

        void Handle(CheckBox checkBox)
        {
            // Use IsChecked.
            bool flag = checkBox.IsChecked.Value;

            // Assign Window Title.
            this.Title = "IsChecked = " + flag.ToString();
        }
    }