2013-04-28 115 views
0

現在我正在使用tabcontrol來安排我的UI。首先,我把我的按鈕放在我的tabcontrol之外;然而,當我把按鈕放入tabcontrol時,它給出了消息Object reference not set to an object instance。有誰知道我爲什麼收到這條消息?WPF C中的Tabcontrol#

編輯

下面是我的XAML:

<Window x:Class="StudySystem.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="UI" Height="600" Width="811" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:StudySystem" Loaded="Window_Loaded"> 
    <Grid Width="791"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="129*" /> 
      <RowDefinition Height="432*" /> 
     </Grid.RowDefinitions> 
     <TabControl Margin="2,0,0,42"> 
      <TabItem Header="Book Info" > 
       <Grid ShowGridLines="False"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="100" /> 
         <ColumnDefinition Width="150" /> 
         <ColumnDefinition Width="150" /> 
         <ColumnDefinition Width="178*" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition Height="22*" /> 
        </Grid.RowDefinitions> 
        <TextBlock Text="Book Code:" Height="25" Margin="0,15,0,45"></TextBlock> 
        <TextBox Name="txtCode" Grid.Column="1" Margin="2,15,0,51" 
          Width="148"></TextBox> 
        <TextBlock Grid.Row="1" Text="Title:" Margin="0,1,0,33" Height="18"></TextBlock> 
        <TextBox Name="txtTitle" Grid.Row="1" Grid.Column="1" Margin="2,1,148,32" Grid.ColumnSpan="2"></TextBox> 

        <TextBlock Grid.Row="3" Text="Author:" Margin="0,5,0,33" Height="17"></TextBlock> 
        <TextBox Name="txtAuthor" Grid.Row="3" Grid.Column="1" Margin="0,6,0,30"></TextBox> 
        <Button Content="OK" Grid.Row="4" Grid.Column="1" Margin="0,1,0,37"></Button> 
       </Grid> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 
+1

你的代碼看起來沒問題。問題是別的地方。 – 2013-04-28 13:20:36

+0

只是看看異常的堆棧跟蹤http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – 2013-04-28 13:21:24

+0

你在後面的代碼中改變了什麼? – MichaelS 2013-04-28 13:21:39

回答

1

我以前見過這個,它的代碼創建表單之前,它引用您的形式的東西。檢查你打電話的順序。

+0

但它只是在intializecompnent函數損壞 – CYB 2013-04-28 14:53:04

0

深入瞭解Windows標籤我已經加入此代碼對我來說它的做工精細...

<Grid Width="auto"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="432*" /> 
    </Grid.RowDefinitions> 
    <TabControl Grid.Row="1"> 
     <TabItem Header="Book Info" > 
      <Grid ShowGridLines="False"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="100" /> 
        <ColumnDefinition Width="150" /> 
        <ColumnDefinition Width="150" /> 
        <ColumnDefinition Width="178*" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="22*" /> 
       </Grid.RowDefinitions> 
       <TextBlock Text="Book Code:" Height="25" Margin="0,15,0,45"> </TextBlock> 
       <TextBox Name="txtCode" Grid.Column="1" Margin="2,15,0,51" 
         Width="148"></TextBox> 
       <TextBlock Grid.Row="1" Text="Title:" Margin="0,1,0,33" Height="18"></TextBlock> 
       <TextBox Name="txtTitle" Grid.Row="1" Grid.Column="1" Margin="2,1,148,32" Grid.ColumnSpan="2"></TextBox> 

       <TextBlock Grid.Row="3" Text="Author:" Margin="0,5,0,33" Height="17"></TextBlock> 
       <TextBox Name="txtAuthor" Grid.Row="3" Grid.Column="1" Margin="0,6,0,30"></TextBox> 
       <Button Content="OK" Grid.Row="4" Grid.Column="1" Margin="0,1,0,37"></Button> 
       </Grid> 
      </TabItem> 
     </TabControl> 
    </Grid> 

你在window_loaded提到?

+0

我加載我的數據庫在window_loaded – CYB 2013-04-28 14:45:40