Scrollbar in WPF - c#

I am trying to design a page in WPF that requires a scroll bar as there is a lot of contet on this particular page. Since there are different types of infomration that need to be inputted, I have put the different parts in different grids. I have then set the ScrollViewer and put all the grids inside this but when I run it it does not work. Here is my code:
<Page x:Class="uniFirstGo.AddStudent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mosque"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="AddStudent">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid Margin="0,0,0,-751">
<Grid.RowDefinitions>
<RowDefinition Height="593*"/>
<RowDefinition Height="154*"/>
<RowDefinition Height="46*"/>
<RowDefinition Height="44*"/>
</Grid.RowDefinitions>
<Label Name="ChildsDetails" Content="Childs Details" HorizontalAlignment="Left" Height="24" Margin="350,10,0,0" VerticalAlignment="Top" Width="91"/>
<Grid HorizontalAlignment="Left" Height="178" Margin="20,54,0,0" VerticalAlignment="Top" Width="770">
<Label x:Name="ChildsSurname" Content="Childs Surname" HorizontalAlignment="Left" Height="24" Margin="10,14,0,0" VerticalAlignment="Top" Width="97"/>
<TextBox x:Name="childsSurname" HorizontalAlignment="Left" Height="24" Margin="107,14,0,0" VerticalAlignment="Top" Width="145" SpellCheck.IsEnabled="True"/>
<Label x:Name="ChildsFirstName" Content="Childs First Name" HorizontalAlignment="Left" Height="24" Margin="276,14,0,0" VerticalAlignment="Top" Width="105"/>
<TextBox x:Name="childsFirstName" HorizontalAlignment="Left" Height="24" Margin="386,14,0,0" VerticalAlignment="Top" Width="155"/>
<Label Content="Address" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.605,0.288" Width="97"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="107,54,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="468" Grid.ColumnSpan="2"/>
<Label Content="Date of Birth
" HorizontalAlignment="Left" Margin="10,92,0,0" VerticalAlignment="Top" Height="25" Width="83"/>
<DatePicker HorizontalAlignment="Left" Margin="107,93,0,0" VerticalAlignment="Top" Width="130"/>
<Label Content="Postcode" HorizontalAlignment="Left" Margin="600,54,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.368,-0.154"/>
<TextBox x:Name="childsGender_Copy" HorizontalAlignment="Left" Height="24" Margin="663,54,0,0" VerticalAlignment="Top" Width="97"/>
<Label Content="Age" HorizontalAlignment="Left" Margin="10,132,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="107,132,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="130"/>
<Label Content="Gender
" HorizontalAlignment="Left" Margin="578,10,0,0" VerticalAlignment="Top" Height="26"/>
<CheckBox Content="Male
" HorizontalAlignment="Left" Margin="651,16,0,0" VerticalAlignment="Top"/>
<CheckBox Content="Female" HorizontalAlignment="Left" Margin="702,16,0,0" VerticalAlignment="Top"/>
</Grid>
<Label Content="Parents Details" HorizontalAlignment="Left" Margin="350,249,0,0" VerticalAlignment="Top"/>
<Grid HorizontalAlignment="Left" Height="201" Margin="10,280,0,0" VerticalAlignment="Top" Width="780">
<Label Content="Fathers Full Name
" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="134" Height="28" RenderTransformOrigin="0.5,0.5"></Label>
<Label Content="Mothers Full Name" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="120" Height="29"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="130,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="138"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="130,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="138"/>
<Label Content="Fathers Address
" HorizontalAlignment="Left" Margin="10,80,0,0" VerticalAlignment="Top" Width="107" Height="28"/>
<Label Content="Mothers Address
" HorizontalAlignment="Left" Margin="10,113,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.132,-0.154" Width="107" Height="28"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="130,85,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="640"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="130,118,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="640"/>
<Label Content="Fathers Phone Number
" HorizontalAlignment="Left" Margin="283,12,0,0" VerticalAlignment="Top" Height="26"/>
<Label Content="Mothers Phone Number
" HorizontalAlignment="Left" Margin="283,46,0,0" VerticalAlignment="Top" Height="26"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="426,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="426,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="Mothers Date of Birth" HorizontalAlignment="Left" Margin="554,48,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.48,0.077"/>
<DatePicker HorizontalAlignment="Left" Margin="679,15,0,0" VerticalAlignment="Top" Width="101"/>
<DatePicker HorizontalAlignment="Left" Margin="679,48,0,0" VerticalAlignment="Top" Width="101"/>
<Label Content="Father Date of Birth
" HorizontalAlignment="Left" Margin="554,16,0,0" VerticalAlignment="Top" Height="25"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="130,153,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="253"/>
<Label Content="Fathers Email" HorizontalAlignment="Left" Margin="10,150,0,0" VerticalAlignment="Top" Width="107"/>
<Label Content="Mothers Email" HorizontalAlignment="Left" Margin="405,150,0,0" VerticalAlignment="Top" Width="107"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="517,153,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="253"/>
</Grid>
<Label Content="Emergency Contact Details
" HorizontalAlignment="Left" Margin="328,486,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.895,-2.308" Height="29"/>
<Grid HorizontalAlignment="Left" Height="82" Margin="10,520,0,0" VerticalAlignment="Top" Width="780" Grid.RowSpan="2">
<Label Content="Surname
" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" Height="29"/>
<Label Content="Surname
" HorizontalAlignment="Left" Margin="10,44,0,0" VerticalAlignment="Top" Width="88" Height="29"/>
<Label Content="First Name
" HorizontalAlignment="Left" Margin="258,44,0,0" VerticalAlignment="Top" Width="88" Height="29" RenderTransformOrigin="0.114,0.448"/>
<Label Content="First Name
" HorizontalAlignment="Left" Margin="258,10,0,0" VerticalAlignment="Top" Width="88" Height="29"/>
<Label Content="Contact Number
" HorizontalAlignment="Left" Margin="513,44,0,0" VerticalAlignment="Top" Width="98" Height="29"/>
<Label Content="Contact Number
" HorizontalAlignment="Left" Margin="513,10,0,0" VerticalAlignment="Top" Width="98" Height="29"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="618,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="74,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="336,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="74,16,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="618,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="336,50,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/>
</Grid>
<Label Content="Medical, Disability and Special Needs
" HorizontalAlignment="Left" Margin="298,617,0,0" VerticalAlignment="Top" Height="26"/>
<Grid HorizontalAlignment="Left" Height="145" Margin="10,648,0,0" VerticalAlignment="Top" Width="780">
<Label Content="Name of Surgery
" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="34"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="116,12,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="175"/>
<Label Content="Surgery Telephone Number
" HorizontalAlignment="Left" Margin="337,10,0,0" VerticalAlignment="Top" Height="34"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="512,12,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258"/>
<Label Content="Surgery Address" HorizontalAlignment="Left" Margin="10,44,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.289,-0.038" Width="100" Height="31"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="116,49,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="654"/>
</Grid>
</Grid>
</ScrollViewer>
</Page>
Can someone tell me whats going wrong here and why the scroll bar isnt working please?
Thanks.

I set your page as the Content of my WPF main window, which has dimensions of 300x400. And this is what I get:
As you can see the horizontal scroll bar is visible and working quite fine, but the vertical isn't.
But note that for some reason you've set your Grid.Margin to an unusual -751.
<Grid Margin="0,0,0,-751">
The four numbers in Margin are defined as follows:
<Grid Margin="left, top, right, bottom">
So let's take a simpler example. I have the following two Grid controls, one inside another, inside my main window.
<Window x:Class="StackOverflowWPF.MainWindow"
...
Title="MainWindow" Height="300" Width="400">
<Grid Margin="20" Background="LightSeaGreen">
<Grid Margin="5, 10, 15, 20" Background="LightGoldenrodYellow"/>
</Grid>
</Window>
And the designer is like below:
So the inner grid left is 5 pixels from it's container grid, top is 10 from it's container, etc. You get the drift. But see what happens when I set the bottom to a negative number.
<Grid Margin="20" Background="LightSeaGreen">
<Grid Margin="5, 10, 15, -20" Background="LightGoldenrodYellow"/>
</Grid>
Designer:
You're essentially telling XAML that the inner Grid can be outside the outer grid, which kind of defeats the purpose of a margin to begin with.
Now let's go back to your problem. A scroll bar lets you scroll contents in case content is larger than the container. But by speficying a negative margin, you're saying your grid can go 751 pixels below the bottom, essentially rendering the scroll bar useless.
Instead, look what happens you set the margin to a reasonable 0.
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid Margin="0,0,0,0">
The output:

Just remove the Margin="0,0,0,-751" in the main grid:
<Page x:Class="uniFirstGo.AddStudent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:mosque"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="AddStudent">
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>....

Related

C# WPF Button "Content" is not displaying

<Grid RenderTransformOrigin="0.5,0.5">
<Grid Margin="0,0,0.286,0.429" Name="LogGrid">
<PasswordBox x:Name="Password" HorizontalAlignment="Left" Margin="130,130,0,0" VerticalAlignment="Top" Width="140"/>
<TextBox x:Name="Login" HorizontalAlignment="Left" Height="20" Margin="140,90,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnSignIn" Content="Sign In" HorizontalAlignment="Left" Margin="130,170,0,0" VerticalAlignment="Top" Width="65"/>
<Button x:Name="btnSignUp" Content="Sign Up" HorizontalAlignment="Left" Margin="205,170,0,0" VerticalAlignment="Top" Width="65"/>
</Grid>
<Grid Name="MainPart" Width="800" Height="600" Visibility="Hidden" HorizontalAlignment="Right">
<TextBox x:Name="Chat" HorizontalAlignment="Left" Height="540" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="800" AcceptsReturn="True" IsReadOnly="True"/>
<TextBox Name="SendString" Width="740" Height="60" Margin="0, 540, 0, 0" HorizontalAlignment="Left"/>
<Button x:Name="SendMsg" Margin="740, 540, 0, 0" Width="60" Height="60" HorizontalAlignment="Right" Content="Send" /> <-!!!!!!!!!!!!!!
</Grid>
The problem is: Button from 2nd Grid, named as "SendMsg" not displays "Content"
I just had this issue because I had set a Height attribute of 30, and also a Padding attribute of 15, so my Content had been padded right out of the visible space. :)
Try this
<Button x:Name="SendMsg" Margin="740, 540, 0, 0" Width="60" Height="60" HorizontalAlignment="Right" Text="Send" />
Sure about Visibility="Hidden"? I've tested your code (just changed Visibility="Visible") and everything is displayed correctly.

Columns don't appear before going to TabControl

I making program in wpf that use database and bind value to datagrid. I have two TabControl, and datagrid exist in second. I want to fill it after window loaded and make first column invisible. But although after fill there are items but no columns before select second tab. Why it so?
This is my xaml code
<Window x:Class="AdminControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AdminControl"
mc:Ignorable="d"
Title="MainWindow" Height="513.5" Width="994" Loaded="MainWindowLoaded">
<Grid>
<TabControl>
<TabItem Header="First">
<Grid>
<TextBox x:Name="Product_BarCode" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Артикул" VerticalAlignment="Top" Width="120" Margin="32,28,0,0"/>
<TextBox x:Name="Product_Name" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Название" Width="120" Margin="185,28,0,191"/>
<TextBox x:Name="Product_Price" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Цена" VerticalAlignment="Top" Width="120" Margin="185,114,0,0"/>
<TextBox x:Name="Product_Keywords" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Ключевые слова" VerticalAlignment="Top" Width="120" Margin="32,114,0,0"/>
<TextBox x:Name="Product_TabTitle" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Заголовок вкладки" VerticalAlignment="Top" Width="120" Margin="823,69,0,0"/>
<TextBox x:Name="Product_Description_Title" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Заголовок Описания" VerticalAlignment="Top" Width="120" Margin="658,69,0,0"/>
<TextBox x:Name="Product_Link" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="ССылка" VerticalAlignment="Top" Width="120" Margin="494,69,0,0"/>
<TextBox x:Name="Product_Length" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Длина" VerticalAlignment="Top" Width="120" Margin="32,69,0,0"/>
<TextBox x:Name="Product_Height" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Высота" VerticalAlignment="Top" Width="120" Margin="823,28,0,0"/>
<TextBox x:Name="Product_Width" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Ширина" VerticalAlignment="Top" Width="120" Margin="658,28,0,0"/>
<TextBox x:Name="Product_Weight" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Вес" Width="120" Margin="494,28,0,433"/>
<TextBox x:Name="Product_Count" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Количество" Width="120" Margin="337,28,0,191"/>
<TextBox x:Name="Product_Image" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Название изображения" VerticalAlignment="Top" Width="152" Margin="337,114,0,0"/>
<TextBox x:Name="Product_Description" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Описание" VerticalAlignment="Top" Width="120" Margin="185,69,0,0"/>
<TextBox x:Name="Product_SearchDescription" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="Описание Поисковика" VerticalAlignment="Top" Width="120" Margin="337,69,0,0"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="134,17,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</TabItem>
<TabItem Header="Second">
<Grid>
<DataGrid x:Name="categoryGrid" HorizontalAlignment="Left" Margin="875,10,0,0" VerticalAlignment="Top" Height="388" Width="91" InitializingNewItem="categoryGridAddRow" IsEnabled="True"/>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" Height="464" Width="860" Margin="10,10,0,-22" Grid.Column="0" Initialized="sadfasd"/>
<Button x:Name="button1" Content="Button" Margin="903,422,-2,0" VerticalAlignment="Top" Click="button1_Click"/>
</Grid>
</TabItem>
</TabControl>
</Grid>

Textboxes don't work Windows Phone 8.1 App

I am not able to have a user input in a textbox when click it both in the Emulator and in windows phone device. I have deleted all the textboxes and replace them with new ones without any special preferences and still don't work. I have implemented the whole interface of my application and need help to solve this issue and start playing with the server connection. Also the rest of the controls all work properly. Please any ideas?
This is the corresponding interface of the code I am posting below.
This is my mainpage.xaml code: If you need anything else please let me know.
<controls:ViewPage
x:Class="Menu_and_Topbar_app__Windows_Phone_8._1_3.MainPage"
xmlns:controls="using:Menu_and_Topbar_app__Windows_Phone_8._1_3.Controls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Menu_and_Topbar_app__Windows_Phone_8._1_3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="#FF250D5C">
<ScrollViewer HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="5.857,6.897,8.265,12.783" RenderTransformOrigin="0.5,0.5" Height="620.32" UseLayoutRounding="False" Width="385.878" d:LayoutRounding="Auto">
<ScrollViewer.RenderTransform>
<CompositeTransform Rotation="0.309"/>
</ScrollViewer.RenderTransform>
<Pivot Name="MyPivot" IsEnabled="True" IsTapEnabled="True">
<Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" FontSize="37" SelectionHighlightColor="#FF898888" />
</DataTemplate>
</Pivot.HeaderTemplate>
<PivotItem x:Name="PivotItem1" Header="Sign In" IsEnabled="True" IsTapEnabled="True">
<Grid HorizontalAlignment="Left" Height="496" Margin="18,19,0,0" VerticalAlignment="Top" Width="311" IsTapEnabled="True" IsRightTapEnabled="True">
<Image x:Name="image" HorizontalAlignment="Left" Height="70" Margin="64,9,0,0" VerticalAlignment="Top" Width="193" Source="/Menu and Topbar app (Windows Phone 8.1)3/Pictures/logo.png" Visibility="Visible" />
<Button x:Name="button" Content="Log In" HorizontalAlignment="Left" Margin="98,409,0,0" VerticalAlignment="Top" Click="button_Click_1"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="16,113,0,0" TextWrapping="Wrap" Text="Username or Email:" VerticalAlignment="Top" FontSize="16"/>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="16,210,0,0" TextWrapping="Wrap" Text="Password:" VerticalAlignment="Top" FontSize="16"/>
<HyperlinkButton Content="Sign Up for SCP?" HorizontalAlignment="Left" Margin="17,266,0,0" VerticalAlignment="Top" FontSize="13.333" NavigateUri="http://scponline.azurewebsites.net/Home/LoginPartialView"/>
<CheckBox x:Name="checkBox" Content="Remember me?" HorizontalAlignment="Left" Margin="17,312,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="17,137,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="275" GotFocus="textBox_GotFocus" AllowDrop="True" />
</Grid>
</PivotItem>
<PivotItem x:Name="PivotItem2" Header="Sign Up" >
<Grid HorizontalAlignment="Left" Height="496" Margin="18,19,0,0" VerticalAlignment="Top" Width="311">
<Button Content="Sign Up" HorizontalAlignment="Left" Margin="97,437,0,0" VerticalAlignment="Top" Click="Button_Click_2"/>
<TextBlock x:Name="textBlock3" HorizontalAlignment="Left" Margin="16,108,0,0" TextWrapping="Wrap" Text="Email:" VerticalAlignment="Top" FontSize="16"/>
<TextBlock x:Name="textBlock4" HorizontalAlignment="Left" Margin="16,168,0,0" TextWrapping="Wrap" Text="Password:" VerticalAlignment="Top" FontSize="16"/>
<TextBox x:Name="textBox3" HorizontalAlignment="Left" Margin="16,126,0,0" TextWrapping="Wrap" Text="username#mail.com" VerticalAlignment="Top" Height="0" Width="276" FontSize="17.593" RenderTransformOrigin="0.5,0.5">
<TextBox.RenderTransform>
<CompositeTransform ScaleY="0.9"/>
</TextBox.RenderTransform>
</TextBox>
<TextBox x:Name="textBox4" HorizontalAlignment="Left" Margin="16,187,0,0" TextWrapping="Wrap" Text="***********" VerticalAlignment="Top" Height="0" Width="276" TextChanged="textBox2_TextChanged" FontSize="17.593" RenderTransformOrigin="0.5,0.5">
<TextBox.RenderTransform>
<CompositeTransform ScaleY="0.9"/>
</TextBox.RenderTransform>
</TextBox>
<Image x:Name="image1" HorizontalAlignment="Left" Height="70" Margin="64,9,0,0" VerticalAlignment="Top" Width="193" Source="/Menu and Topbar app (Windows Phone 8.1)3/Pictures/logo.png"/>
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="16,232,0,0" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Top" SelectionChanged="textBlock1_SelectionChanged" FontSize="16"/>
<TextBox x:Name="textBox5" HorizontalAlignment="Left" Margin="16,250,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="0" Width="276" TextChanged="textBox2_TextChanged" FontSize="17.593" RenderTransformOrigin="0.5,0.5">
<TextBox.RenderTransform>
<CompositeTransform ScaleY="0.9"/>
</TextBox.RenderTransform>
</TextBox>
<TextBlock x:Name="textBlock5" HorizontalAlignment="Left" Margin="16,292,0,0" TextWrapping="Wrap" Text="Name:" VerticalAlignment="Top" SelectionChanged="textBlock1_SelectionChanged" FontSize="16"/>
<TextBox x:Name="textBox6" HorizontalAlignment="Left" Margin="16,310,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="0" Width="276" TextChanged="textBox2_TextChanged" FontSize="17.593" RenderTransformOrigin="0.5,0.5">
<TextBox.RenderTransform>
<CompositeTransform ScaleY="0.9"/>
</TextBox.RenderTransform>
</TextBox>
</Grid>
</PivotItem>
</Pivot>
</ScrollViewer>
</Grid>

WPF create UserControl for a TabItem

I need help at replacing some xaml-code with user control.
I have two tabs in main window and I would like to put the second tab (shown with Header=WCF) in user control.
Here is my code:
<Window x:Class="TesterTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helpers="clr-namespace:TesterTool"
xmlns:da ="clr-namespace:TesterTool.DatabaseAccess"
xmlns:converters="clr-namespace:TesterTool.Converters"
xmlns:vm="clr-namespace:TesterTool"
Title="TestingTool v1.00" Height="350" Width="725"
Icon="Icon.ico">
<Window.Resources>
<converters:BoolVisibilityConverter x:Key="BoolVisibilityConverter" />
<converters:InvertedBoolVisibilityConverter x:Key="InvertedBoolVisibilityConverter"/>
</Window.Resources>
<TabControl>
<TabItem Header="WPF" Margin="-2,0" Height="22" VerticalAlignment="Bottom">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="image.png"/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image gif:ImageBehavior.AnimatedSource="332.gif"
Width="152"
Height="39"
Margin="10,69,194,37"
Grid.Row="1"
Visibility="{Binding IsLoading, Converter={StaticResource BoolVisibilityConverter}}"/>
<TextBox x:Name="tb1"
Text ="{Binding BoxSerialNumber}"
Height="23" Margin="128,26,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
HorizontalAlignment="Left" Width="220"
/>
<TextBox x:Name="tbn2"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource InvertedBoolVisibilityConverter}}"
Height="23" Margin="128,73,8,0"
TextWrapping="Wrap"
Text="{Binding HardwareID}"
VerticalAlignment="Top"
RenderTransformOrigin="0.494,0.478"/>
<TextBox x:Name="tbb3"
HorizontalAlignment="Left"
Height="23"
Margin="128,116,0,0"
TextWrapping="Wrap"
Text="{Binding CompanyContext}"
VerticalAlignment="Top" Width="220"/>
<Label Content="Box Serial Number" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<Label Content="Hardware ID" HorizontalAlignment="Left" Margin="0,73,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<Label Content="Company Context" HorizontalAlignment="Left" Margin="3,113,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5"/>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsTrafficLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Start Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp1"
SelectedDate="{Binding StartDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
<Label Content="End Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp2"
SelectedDate="{Binding EndDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsTransmissionLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Start Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp13"
SelectedDate="{Binding StartDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
<Label Content="End Date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp23"
SelectedDate="{Binding EndDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsCreditLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
<Label Content="Log date"
HorizontalAlignment="Left"
Grid.Row="1"
VerticalAlignment="Top"/>
<DatePicker x:Name="dp12"
SelectedDate="{Binding LogDate}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="25" Width="102"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource BoolVisibilityConverter}}">
</StackPanel>
<Label Content="VesselIMO" HorizontalAlignment="Left" Margin="32,70,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.526,0.5" Grid.Column="1"/>
<Label Content="Select Type" Grid.Column="1" HorizontalAlignment="Left" Margin="32,22,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name ="cb"
ItemsSource="{Binding LogTypes}"
SelectedItem="{Binding SelectedItem}"
Margin="164,26,23,0" VerticalAlignment="Top"
Grid.Column="1">
</ComboBox>
<TextBox x:Name="tb3_Copy"
Visibility="{Binding IsViewLogSelected, Converter={StaticResource InvertedBoolVisibilityConverter}}"
HorizontalAlignment="Left"
Height="23"
Margin="164,73,0,0"
TextWrapping="Wrap"
Text="{Binding VesselIMO}"
VerticalAlignment="Top"
Width="172" Grid.Column="1"/>
<Button Name ="Button1"
Content="Submit"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="197,81,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="152"
Height="39"
Click="Button1Clicked"
IsEnabled="{Binding IsLoading, Converter={StaticResource InvertedBoolVisibilityConverter}}"/>
<TextBox Grid.Column="1"
HorizontalAlignment="Left"
Height="23"
Margin="164,120,0,0"
TextWrapping="Wrap"
Text ="{Binding Counter}"
VerticalAlignment="Top"
Width="91"/>
<Label Content="Number of rows" Grid.Column="1" HorizontalAlignment="Left" Margin="32,116,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="WCF">
<Grid Background="#FFE5E5E5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="User Credentials" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="96"/>
<Label Content="Username" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="63"/>
<Label Content="Password" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top" Grid.ColumnSpan="2" Height="26" Width="60"/>
<TextBox x:Name="unTB"
Text="{Binding Username}"
HorizontalAlignment="Left"
Height="22"
Margin="103,73,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<Label Content="Box ID"
HorizontalAlignment="Left"
Margin="384,73,0,0"
VerticalAlignment="Top"
Grid.Column="1"
Height="26"
Width="44"/>
<Label Content="Software Version" HorizontalAlignment="Left" Margin="384,124,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="99"/>
<TextBox x:Name="boxTB"
Text="{Binding BoxID}"
HorizontalAlignment="Left"
Height="22"
Margin="501,73,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<TextBox
Text ="{Binding SoftwareId}"
x:Name="SIBox"
HorizontalAlignment="Left"
Height="22"
Margin="501,124,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
<Button Content="Create Box" HorizontalAlignment="Left" Margin="538,233,0,0" VerticalAlignment="Top" Width="142" Height="35" Click="Button_Click" Grid.Column="1" Command="{Binding CreateBoxCommand}"/>
<TextBox x:Name="pass"
Text ="{Binding Password}"
HorizontalAlignment="Left"
Height="22"
Margin="103,120,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="179"
Grid.Column="1"/>
</Grid>
</TabItem>
</TabControl>
Does anyone knows how to do this in proper way?
Put entire Grid and its contents present in your TabItem(WCF) in a user-control, and name this UserControl as WCFTabUserControl.
Reference the namespace in your Window tag like : xmlns:uc="clr-namespace:WpfApplication1"
Use your user-control :
<TabItem Header="WCF">
<uc:WCFTabUserControl/>
</TabItem>

"Object reference not set to an instance of an object" error showing on design load in WPF

I have seen many questions on the same topic here, and tried almost all solutions recommended, But still not getting it cleared. So please kindly let me know any new updates on this.
<UserControl x:Class="ClinicManagement_Forms.ClinicDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:view="clr-namespace:ClinicManagement_Forms"
mc:Ignorable="d" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="875" Height="474">
<UserControl.DataContext>
<view:ClinicRegistrationViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="LeftErrorTemplate">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding AdornedElement.(Validation.Errors).[0].ErrorContent, ElementName=ErrorAdorner}" Background="LightCoral" Foreground="White" VerticalAlignment="Center"/>
<AdornedElementPlaceholder x:Name="ErrorAdorner">
<Border BorderBrush="LightCoral" BorderThickness="1" />
</AdornedElementPlaceholder>
</StackPanel>
</ControlTemplate>
</Grid.Resources>
<TextBlock HorizontalAlignment="Left" Margin="2,0,0,444" TextWrapping="Wrap" Text="Clinic Registration" VerticalAlignment="Bottom" Width="873" Height="30" TextAlignment="Center" FontSize="18" FontFamily="Showcard Gothic" />
<TextBlock HorizontalAlignment="Left" Margin="91,99,0,0" TextWrapping="Wrap" Text="Clinic Name" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="202,96,0,0" TextWrapping="Wrap" Text="{Binding CliicRegistation.ClinicName , UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Margin="91,136,0,0" TextWrapping="Wrap" Text="Address" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" VerticalScrollBarVisibility="Visible" Height="58" Margin="202,133,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AcceptsReturn="True"/>
<TextBlock HorizontalAlignment="Left" Margin="91,210,0,0" TextWrapping="Wrap" Text="Place" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="202,207,0,0" TextWrapping="Wrap" Text="{Binding CliicRegistation.Place, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Margin="91,250,0,0" TextWrapping="Wrap" Text="Phone No" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="202,247,0,0" TextWrapping="Wrap" Text="{Binding CliicRegistation.PhonNo , UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}" VerticalAlignment="Top" Width="200" IsReadOnlyCaretVisible="True"/>
<TextBlock HorizontalAlignment="Left" Margin="91,289,0,0" TextWrapping="Wrap" Text="User Name" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="202,286,0,0" TextWrapping="Wrap" Text="{Binding CliicRegistation.UserName, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Margin="91,329,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/>
<PasswordBox HorizontalAlignment="Left" Height="23" Margin="202,329,0,0" Name="passwordBox1" Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Margin="91,367,0,0" TextWrapping="Wrap" Text="Confirm Password" VerticalAlignment="Top"/>
<PasswordBox HorizontalAlignment="Left" Height="23" Margin="202,367,0,0" PasswordChar="{Binding CliicRegistation.ConformPassword, Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}" VerticalAlignment="Top" Width="200"/>
<Button IsEnabled="{Binding CliicRegistation.AllPropertiesValid}" Content="Save" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="202,441,0,0" VerticalAlignment="Top" Width="75"/>
<Button IsEnabled="{Binding CliicRegistation.AllPropertiesValid}" Content="Delete" HorizontalAlignment="Left" Margin="299,441,0,0" VerticalAlignment="Top" Width="75"/>
<Button Content="Clear" HorizontalAlignment="Left" Margin="397,441,0,0" VerticalAlignment="Top" Width="75"/>
<Image HorizontalAlignment="Left" Height="100" Margin="536,89,0,0" VerticalAlignment="Top" Width="100"/>
<Image HorizontalAlignment="Left" Height="100" Margin="561,89,0,0" VerticalAlignment="Top" Width="148" OpacityMask="#FFD44949"/>
<Button Content="Select" HorizontalAlignment="Left" Margin="559,207,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="5.053,-10.4"/>
<Button Content="Clear" HorizontalAlignment="Left" Margin="647,208,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="5.053,-10.4"/>
</Grid>
It is the code I had tried, when i click on design mode, This error is shown.
Thanks in advance

Categories