2011-09-05 36 views
1

我在調用checkBox2_Checked的代碼中收到NullReferenceException。異常表示stackPanelListbox爲空。它在XAML中聲明,並且類似聲明的stackPanel不爲null。這裏有什麼問題?爲什麼我的StackPanel不在生成的類中? (NullReferenceException)

這裏的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; 
     } 
    } 
} 

回答

1

當異常被拋出?在窗口啓動時,還是在按下CheckBox之後?

如果它在啓動時,可能是因爲checkBox2_Checked()在initializecompenents()期間被調用,而stackPanelListbox尚未被聲明。

+0

在窗口啓動過程中發生異常。我不認爲事件可能發生在用戶真正點擊它之前。我會無效檢查方法,並與您取回 – jdphenix

+0

這就是它。出於某種原因,Checkbox.Checked會在initializeComponent()期間被觸發,而Checkbox.Clicked不會,所以我將其更改爲。 – jdphenix

0

嘗試從XAML中刪除列表框中Grid.Column = 2

1

我認爲checkBox2.IsCheckednull
試試這個:

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

如果你只想隱藏或在事件處理顯示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> 

這種方法允許你刪除的事件處理程序。所以你不會遇到初始化順序錯誤。

相關問題