CYB
CYB

Reputation: 1106

Tabcontrol in WPF C#

now I'm using the tabcontrol to arrange my UI. At the first, I put my button outside my tabcontrol; however, when I put the button into the tabcontrol, it gave message, Object reference not set to an object instance. Does anyone know why I got this message ?

edited

Below is my 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>

Upvotes: 0

Views: 2040

Answers (2)

Shafqat Masood
Shafqat Masood

Reputation: 2570

Inside Windows tag i have added this code and for me its working fine...

   <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>

what you have mentioned in window_loaded??

Upvotes: 0

Badmiral
Badmiral

Reputation: 1589

I've seen this before, its code that references things in your form before you create the form. Check the order of what you are calling.

Upvotes: 1

Related Questions