【问题标题】:Why is my StackPanel not in the generated class? (NullReferenceException) [duplicate]为什么我的 StackPanel 不在生成的类中? (NullReferenceException)[重复]
【发布时间】:2011-09-05 07:54:16
【问题描述】:

在调用 checkBox2_Checked 时,我的代码中出现 NullReferenceException。异常表明 stackPanelListbox 为空。它在 XAML 中声明,并且类似声明的 stackPanel 不为空。这里有什么问题?

这是 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#代码:

using System.Windows;

namespace ch0103.WPF
{
    /// <summary>
    /// Interaction logic for LayoutWindow.xaml
    /// </summary>
    public partial class LayoutWindow : Window
    {
        public LayoutWindow() {
            InitializeComponent();
        }

        private void checkBox_Checked(object sender, RoutedEventArgs e) {
            stackPanelButtons.Visibility = (bool) checkBox1.IsChecked ? 
                Visibility.Visible : Visibility.Collapsed;
        }

        private void checkBox2_Checked(object sender, RoutedEventArgs e) {
            stackPanelListbox.Visibility = (bool) checkBox2.IsChecked ? // stackPanelListbox is null here? 
                Visibility.Visible : Visibility.Collapsed;
        }
    }
}

【问题讨论】:

    标签: c# wpf windows xaml


    【解决方案1】:

    什么时候抛出异常?在窗口启动时,还是在您按下 CheckBox 之后?

    如果是在启动时,可能是因为在initializecompenents()期间调用了checkBox2_Checked(),而stackPanelListbox还没有声明。

    【讨论】:

    • 窗口启动时发生异常。在用户真正点击它之前,我认为该事件不会发生。我将检查方法并与您联系
    • 就是这样。出于某种原因,在 initializeComponent() 期间会触发 Checkbox.Checked,而 Checkbox.Clicked 不会,所以我将其更改为。
    【解决方案2】:

    我认为checkBox2.IsChecked 是null。
    试试这个:

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

    【讨论】:

      【解决方案3】:

      如果您只想在事件处理程序中隐藏或显示 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>
      

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

      【讨论】:

        【解决方案4】:

        尝试从 ListBox 中的 xaml 中删除 Grid.Column = 2。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-30
          • 2011-09-02
          • 2010-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多