0

I learning WPF and I have a sample code with check box as follows

I want all the state is checked

But when I added ischecked = "true" to the Enable All check box there was nullexception error

I tried for all the IsChecked attribute check box as true but also nullexception error

If all the checkboxes are IsChecked ="false", they operate normally

Anyone can explain to me why so?

Thank you so much

<Window x:Class="Check_box_test1.MainWindow"
    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"
    xmlns:local="clr-namespace:Check_box_test1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="10">
    <Label FontWeight="Bold">Application Options</Label>
    <StackPanel Margin="10,5">
        <CheckBox IsChecked="True" IsThreeState="True" Name="cbAllFeatures" Checked="cbAllFeatures_CheckedChanged" Unchecked="cbAllFeatures_CheckedChanged" Click="cbAllFeatures_Click">Enable all</CheckBox>
        <StackPanel Margin="20,5">
            <CheckBox Name="cbFeatureAbc"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature ABC</CheckBox>
            <CheckBox Name="cbFeatureXyz"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature XYZ</CheckBox>
            <CheckBox Name="cbFeatureWww"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature WWW</CheckBox>
        </StackPanel>
    </StackPanel>
</StackPanel>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void cbAllFeatures_CheckedChanged(object sender, RoutedEventArgs e)
    {
        bool newVal = (cbAllFeatures.IsChecked == true);
        cbFeatureAbc.IsChecked = newVal;
        cbFeatureXyz.IsChecked = newVal;
        cbFeatureWww.IsChecked = newVal;
    }

    private void cbFeature_CheckedChanged(object sender, RoutedEventArgs e)
    {
        cbAllFeatures.IsChecked = null;
        if ((cbFeatureAbc.IsChecked == true) && (cbFeatureXyz.IsChecked == true) && (cbFeatureWww.IsChecked == true))
            cbAllFeatures.IsChecked = true;
        if ((cbFeatureAbc.IsChecked == false) && (cbFeatureXyz.IsChecked == false) && (cbFeatureWww.IsChecked == false))
            cbAllFeatures.IsChecked = false;
    }
    private void cbAllFeatures_Click(object sender, RoutedEventArgs e)
    {
        if (cbAllFeatures.IsChecked != true)
        {
            cbAllFeatures.IsChecked = false;
        }
    }
}
XLee1162
  • 9
  • 5
  • @Jazb: Oh, it makes me understand the controls created in the XAML file. I thought that the InitializeComponent () function was initialized I already know I'm wrong because of the order of arranging the checkboxes. But I wonder if my case should fix code? Do you have any suggestions for me? Thank you! – XLee1162 Jan 28 '22 at 04:30

1 Answers1

0

When you set IsChecked="True" in the xaml, it will trigger cbAllFeatures_CheckedChanged method.

And at that time, the other checkboxes has not initialized, so, call cbFeatureAbc.IsChecked will cause exception.

I fixed the bug by setting cbAllFeatures.IsChecked after InitializeComponent() as below, hope it helps:

MainWindow.xaml.cs

using System.Windows;

namespace TestWpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.cbAllFeatures.IsChecked = true;
        }

        private void cbAllFeatures_CheckedChanged(object sender, RoutedEventArgs e)
        {
            bool newVal = (cbAllFeatures.IsChecked == true);
            cbFeatureAbc.IsChecked = newVal;
            cbFeatureXyz.IsChecked = newVal;
            cbFeatureWww.IsChecked = newVal;
        }

        private void cbFeature_CheckedChanged(object sender, RoutedEventArgs e)
        {
            cbAllFeatures.IsChecked = null;
            if ((cbFeatureAbc.IsChecked == true) && (cbFeatureXyz.IsChecked == true) && (cbFeatureWww.IsChecked == true))
                cbAllFeatures.IsChecked = true;
            if ((cbFeatureAbc.IsChecked == false) && (cbFeatureXyz.IsChecked == false) && (cbFeatureWww.IsChecked == false))
                cbAllFeatures.IsChecked = false;
        }
        private void cbAllFeatures_Click(object sender, RoutedEventArgs e)
        {
            if (cbAllFeatures.IsChecked != true)
            {
                cbAllFeatures.IsChecked = false;
            }
        }
    }
}

MainWindow.xaml

<Window x:Class="TestWpfApp.MainWindow"
    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"
    xmlns:local="clr-namespace:TestWpfApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <StackPanel Margin="10">
        <Label FontWeight="Bold">Application Options</Label>
        <StackPanel Margin="10,5">
            <CheckBox IsThreeState="True" Name="cbAllFeatures" Checked="cbAllFeatures_CheckedChanged" Unchecked="cbAllFeatures_CheckedChanged" Click="cbAllFeatures_Click">Enable all</CheckBox>
            <StackPanel Margin="20,5">
                <CheckBox Name="cbFeatureAbc"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature ABC</CheckBox>
                <CheckBox Name="cbFeatureXyz"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature XYZ</CheckBox>
                <CheckBox Name="cbFeatureWww"  Checked="cbFeature_CheckedChanged" Unchecked="cbFeature_CheckedChanged">Enable feature WWW</CheckBox>
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Window>
cg-zhou
  • 528
  • 3
  • 6