C# 为什么我的StackPanel不在生成的类中?(NullReferenceException)

C# 为什么我的StackPanel不在生成的类中?(NullReferenceException),c#,wpf,windows,xaml,C#,Wpf,Windows,Xaml,在对checkBox2\u Checked的调用中,我的代码中有一个NullReferenceException。异常指示stackPanelListbox为空。它在XAML中声明,并且类似声明的stackPanel不为null。这里怎么了 以下是XAML: <Window x:Class="ch0103.WPF.LayoutWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

在对checkBox2\u Checked的调用中,我的代码中有一个NullReferenceException。异常指示stackPanelListbox为空。它在XAML中声明,并且类似声明的stackPanel不为null。这里怎么了

以下是XAML:

<Window x:Class="ch0103.WPF.LayoutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LayoutWindow" Height="450" Width="900">
    <StackPanel Name="stackPanelMain">
        <WrapPanel>
            <CheckBox Name="checkBox1" VerticalAlignment="Center" IsThreeState="False"     IsChecked="True" Click="checkBox_Checked" Content="Button StackPanel" Margin="0,0,11,0" />
            <CheckBox Content="Listbox StackPanel" Height="16" Name="checkBox2" IsChecked="True" Checked="checkBox2_Checked" />
        </WrapPanel>
        <Grid Name="grid1" ShowGridLines="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel HorizontalAlignment="Left" Name="stackPanelButtons" VerticalAlignment="Top" Visibility="Visible">
                <Button Content="Button" Height="23" Name="button1" />
                <Button Content="Button" Height="23" Name="button2" />
                <Button Content="Button" Height="23" Name="button3" />
            </StackPanel>
            <StackPanel Name="stackPanelListbox" Grid.Column="1">
                <ListBox Grid.Column="2" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200">
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                </ListBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>

这是c代码:

使用System.Windows;
命名空间ch0103.WPF
{
/// 
///LayoutWindow.xaml的交互逻辑
/// 
公共部分类布局窗口:窗口
{
公共布局窗口(){
初始化组件();
}
已选中私有无效复选框(对象发送方、路由目标方){
stackPanelButtons.Visibility=(bool)checkBox1.IsChecked?
可见性.可见:可见性.折叠;
}
私有无效复选框2u已选中(对象发送方,路由目标e){
stackPanelListbox.Visibility=(bool)checkBox2.IsChecked?//此处stackPanelListbox为空?
可见性.可见:可见性.折叠;
}
}
}

尝试从xaml in列表框中删除
Grid.Column=2

我认为
复选框2.IsChecked
null

试试这个:

stackPanelListbox.Visibility = (checkBox2.IsChecked ?? false) ?
                               Visibility.Visible : Visibility.Collapsed;

何时抛出异常?在窗口启动时,还是在按下复选框后


如果它在启动时处于中,可能是因为在InitializeComponents()期间调用了checkBox2_Checked(),而stackPanelListbox尚未声明。

如果您只想在事件处理程序中隐藏或显示StackPanel,则可以使用数据绑定:

<Window x:Class="ch0103.WPF.LayoutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <StackPanel Name="stackPanelMain">
        <WrapPanel>
            <!-- Note the removed event handlers here: -->
            <CheckBox Name="checkBox1" VerticalAlignment="Center" IsThreeState="False" IsChecked="True" Content="Button StackPanel" Margin="0,0,11,0" />
            <CheckBox Content="Listbox StackPanel" Height="16" Name="checkBox2" IsChecked="True" IsThreeState="False" />
        </WrapPanel>
        <Grid Name="grid1" ShowGridLines="True">
            <!-- This one is used to convert checkbox state to visibility -->
            <Grid.Resources>
                <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!-- Additional Binding attributes in both StackPanel elements: -->
            <StackPanel HorizontalAlignment="Left" Name="stackPanelButtons" VerticalAlignment="Top" Visibility="{Binding IsChecked, ElementName=checkBox1, Converter={StaticResource BoolToVisConverter}}">
                <Button Content="Button" Height="23" Name="button1" />
                <Button Content="Button" Height="23" Name="button2" />
            </StackPanel>
            <StackPanel Name="stackPanelListbox" Grid.Column="1" Visibility="{Binding IsChecked, ElementName=checkBox2, Converter={StaticResource BoolToVisConverter}}">
                <ListBox HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200">
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                </ListBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>


这种方法允许您删除事件处理程序。因此,您不会遇到初始化顺序错误。

异常发生在窗口启动期间。直到用户真正点击它,我才认为事件可能发生。我会检查这个方法,然后和你联系这就是它。由于某些原因,在初始化Component()期间会激发Checkbox.Checked,而Checkbox.Clicked则不会,因此我将其更改为该选项。
<Window x:Class="ch0103.WPF.LayoutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <StackPanel Name="stackPanelMain">
        <WrapPanel>
            <!-- Note the removed event handlers here: -->
            <CheckBox Name="checkBox1" VerticalAlignment="Center" IsThreeState="False" IsChecked="True" Content="Button StackPanel" Margin="0,0,11,0" />
            <CheckBox Content="Listbox StackPanel" Height="16" Name="checkBox2" IsChecked="True" IsThreeState="False" />
        </WrapPanel>
        <Grid Name="grid1" ShowGridLines="True">
            <!-- This one is used to convert checkbox state to visibility -->
            <Grid.Resources>
                <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!-- Additional Binding attributes in both StackPanel elements: -->
            <StackPanel HorizontalAlignment="Left" Name="stackPanelButtons" VerticalAlignment="Top" Visibility="{Binding IsChecked, ElementName=checkBox1, Converter={StaticResource BoolToVisConverter}}">
                <Button Content="Button" Height="23" Name="button1" />
                <Button Content="Button" Height="23" Name="button2" />
            </StackPanel>
            <StackPanel Name="stackPanelListbox" Grid.Column="1" Visibility="{Binding IsChecked, ElementName=checkBox2, Converter={StaticResource BoolToVisConverter}}">
                <ListBox HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200">
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                </ListBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>