In edit mode, my textBlocks appear as I want. But when I run the code, the textBlock4 is cropped ! I would like to know why and how to correct this, thank you.
XAML :
<Window x:Class="CuttedControlAfterCompilation.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:CuttedControlAfterCompilation"
mc:Ignorable="d"
Title="Cutted Control After Compilation" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Horizontal" Margin="0,50">
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="73" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
</StackPanel>
</Grid>
</Window>
Images :
textBlock4 with its full width in edit mode
textBlock4 with its width trimmed after execution
If you add WindowStyle="None" to your window you the textblock will no longer be cut. See Window.WindowStyle Property. I believe the reason is the window border is using some of the width
Alternatively set SizeToContent="WidthAndHeight" and you will be set. See Window.SizeToContent Property
So the code would be
<Window x:Class="CuttedControlAfterCompilation.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:CuttedControlAfterCompilation"
mc:Ignorable="d"
Title="Cutted Control After Compilation"
SizeToContent="WidthAndHeight">
<Grid>
<StackPanel Orientation="Horizontal" Margin="0,50">
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="73" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
</StackPanel>
</Grid>
In some cases I had trouble using the SizeToContent property. But after several manipulations, I understand and use it much better now.
Troubles
Trouble with a Grid
<Grid Margin="0,50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148*"/>
<ColumnDefinition Width="148*"/>
<ColumnDefinition Width="148*"/>
<ColumnDefinition Width="75*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Grid.Column="1" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Grid.Column="2" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Grid.Column="3" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
</Grid>
Image : The SizeToContent property narrows the dimensions of the Grid.
Trouble with a StackPanel
<StackPanel Orientation="Horizontal" Margin="0,50">
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="75" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
</StackPanel>
Image : The SizeToContent property narrows the height of the StackPanel.
Corrections
For the Grid
Possibility 1 : Specify the Width and Height of the Grid (Do not leave them in Auto.).
<Grid Width="519" Height="221" Margin="0,50">
Possibility 2 : Set the value of the row in Pixel, as well as the value of each column.
<Grid.RowDefinitions>
<RowDefinition Height="221"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148"/>
<ColumnDefinition Width="148"/>
<ColumnDefinition Width="148"/>
<ColumnDefinition Width="75"/>
</Grid.ColumnDefinitions>
Possibility 3 : Specify the Width and Height of each TextBlock (Do not leave them in Auto.).
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" Height="221" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" Height="221" Grid.Column="1" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" Height="221" Grid.Column="2" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="75" Height="221" Grid.Column="3" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
Possibility 4 : Set the values of the MinWidth and MinHeight to the Width and Height (which are in Auto in this case) of the Grid.
<Grid Margin="0,50" MinWidth="519" MinHeight="221">
For the StackPanel
Possibility 1 : Specify the Height of the StackPanel (Do not leave it in Auto.).
<StackPanel Height="221" Orientation="Horizontal" Margin="0,50">
Possibility 2 : Specify the Height of each TextBlock (Do not leave it in Auto.).
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" Height="221" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" Height="221" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" Height="221" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="75" Height="221" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
Possibility 3 : Set the value of the MinHeight to the Height (which is in Auto in this case) of the StackPanel.
<StackPanel Orientation="Horizontal" Margin="0,50" MinHeight="221">
Conclusion
It appears that the SizeToContent property ignores spaces that are not used in controls when the dimensions are floating (Star or Auto). This can be remedied by specifying the dimensions of the control that the Window contains or those of its children.
So, to take the XAML code from the question, it may be corrected by setting the Grid MinHeight to 319 and setting the SizeToContent = "WidthAndHeight" property to the Window.
<Window x:Class="CuttedControlAfterCompilation.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:CuttedControlAfterCompilation"
mc:Ignorable="d"
Title="Cutted Control After Compilation" SizeToContent = "WidthAndHeight">
<Grid MinHeight="319">
<StackPanel Orientation="Horizontal" Margin="0,50">
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" Width="148" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" Width="73" FontSize="112" FontWeight="Bold" TextAlignment="Center"/>
</StackPanel>
</Grid>
</Window>
I thank #PouyaAbadi very much for his contribution.
This is a standard window issue. You rely on calculated width values, but they gat messed by ThreeD window border sizes. You can make a window wider as Pouya Abadi mentioned, that would be perfect for this case. But it is not a good solution IMHO.
WPF is all about layout. I'd use DockPanel to make contols take up all the space available, or Grid Columns (1*,1*,1*,.75*) sizes. I'm not sure what you are going to make out of this.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width=".75*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="textBlock1" Background="#FFDEE2E4" Text="1" FontSize="48" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock x:Name="textBlock2" Background="#FFC7CCD0" Text="2" FontSize="48" FontWeight="Bold" TextAlignment="Center" Grid.Column="1"/>
<TextBlock x:Name="textBlock3" Background="#FFAEB5B9" Text="3" FontSize="48" FontWeight="Bold" TextAlignment="Center" Grid.Column="2"/>
<TextBlock x:Name="textBlock4" Background="#FFB29AC5" Text="4" FontSize="112" FontWeight="Bold" TextAlignment="Center" Grid.Column="3"/>
</Grid>
I personally don't like standard window and I use a custom one ModernUI.WPF by First Floor Software + my window style, that cleans up that modern madness and gives me full control on how it looks.
Why don't you just increase the width of the window?
<Window ... Title="Cutted Control After Compilation" Height="350" Width="533">
The other option would, as mentioned, be to set the SizeToContent property of the window to WidthAndHeight or just Width:
<Window ... SizeToContent="WidthAndHeight">
This should be a simple fix.
Related
In my serial port WPF application , the labels (BaudRate,Parity,DataBits and stopBit) and the StatusBar (for displaying PortStatus) are in wrong place when I maximise the window.
I would like to have suggestion from expertise to resolve my issue.
So please let me know if I can make any changes/edit on my xaml file to have these orientation on the same place as it is in Normal window mode.
<Window
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:SerialReadAndWrite"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ATR220ReadAndWrite.MainWindow"
mc:Ignorable="d"
Title="ATR220ReadAndWrite" Height="450" Width="800">
<Grid Margin="0,0,-71,-107">
<ComboBox x:Name="ComPortComboBox" HorizontalAlignment="Left" Height="14" Margin="115,20,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ComPort_SelectionChanged" DropDownOpened="ComPort_DropDownOpened" VerticalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="False" FontSize="9" FontFamily="Arial"/>
<Label Content="PortNumber :" HorizontalAlignment="Left" Margin="21,14,0,0" VerticalAlignment="Top"/>
<Label Content="PortSettings :" HorizontalAlignment="Left" Margin="21,58,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
<Image HorizontalAlignment="Left" Height="26" Margin="359,14,0,0" VerticalAlignment="Top" Width="29" RenderTransformOrigin="-0.23,0.36"/>
<ComboBox x:Name="BaudRateComboBox" HorizontalAlignment="Left" Margin="115,84,0,0" VerticalAlignment="Top" Width="98" Height="17" SelectionChanged="BaudRate_SelectionChanged" IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial">
<System:String>115200</System:String>
<System:String>57600</System:String>
<System:String>38400</System:String>
<System:String>19200</System:String>
<System:String>14400</System:String>
<System:String>9600</System:String>
<System:String>4800</System:String>
</ComboBox>
<ComboBox x:Name="ParityComboBox" HorizontalAlignment="Left" Height="17" Margin="308,86,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="Parity_SelectionChanged" IsEnabled="False" FontSize="9" FontFamily="Arial">
<System:String>Even</System:String>
<System:String>Mark</System:String>
<System:String>None</System:String>
<System:String>Odd</System:String>
<System:String>Space</System:String>
</ComboBox>
<ComboBox x:Name="DataBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="115,125,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="DataBits_SelectionChanged" IsEnabled="False" FontSize="8" FontFamily="Arial">
<System:String>5</System:String>
<System:String>6</System:String>
<System:String>7</System:String>
<System:String>8</System:String>
</ComboBox>
<ComboBox x:Name="StopBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="308,123,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="StopBits_SelectionChanged" IsEnabled="False" FontSize="10" FontFamily="Arial">
<System:String>One</System:String>
<System:String>Two</System:String>
<System:String>OnePointFive</System:String>
</ComboBox>
<Label Content="Baudrate :" HorizontalAlignment="Center" Margin="47,81,753,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="63"/>
<Label Content="Parity :" HorizontalAlignment="Center" Margin="234,81,573,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="56"/>
<Label Content="DataBits :" HorizontalAlignment="Center" Margin="48,123,753,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="62"/>
<Label Content="StopBit :" HorizontalAlignment="Center" Margin="238,123,567,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="58"/>
<Button x:Name="ConnectButton" Content="Connect" HorizontalAlignment="Left" Margin="308,20,0,0" VerticalAlignment="Top" Width="75" Click="ConnectButton_Click" IsEnabled="False" Height="14" FontSize="10" FontFamily="Arial"/>
<Label Content="ProtocolCustomWndow :" HorizontalAlignment="Left" Margin="21,172,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
<ComboBox x:Name="ProtocolTypeComboBox" HorizontalAlignment="Left" Height="14" Margin="141,177,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ProtocolTypeComboBox_SelectionChanged" VerticalContentAlignment="Stretch" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" FontSize="9" FontFamily="Arial" IsEnabled="False">
<System:String>WLink</System:String>
</ComboBox>
***<StatusBar HorizontalAlignment="Left" Height="28" Margin="0,396,0,0" VerticalAlignment="Top" Width="794">
<StatusBarItem FontSize="9" FontFamily="Arial" TextOptions.TextHintingMode="Fixed">
<TextBlock Name= "PortStatus"/>
</StatusBarItem>
</StatusBar>***
</Grid>
</Window>
Images of the correct and wrong pics are attached.
wrong position
Wrong Position
Correct position
Correct position
Try something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="PortNumber :" HorizontalAlignment="Center"/>
<ComboBox FontSize="9" FontFamily="Arial" Grid.Column="1">
<ComboBoxItem IsSelected="True">COM 3</ComboBoxItem>
</ComboBox>
<Button Content="Connect" Grid.Row="0" Grid.Column="3"></Button>
<TextBlock Text="PortSettings :" FontSize="10" FontFamily="Arial" Grid.Row="1" HorizontalAlignment="Center"/>
<TextBlock Text="Baudrate :" FontStyle="Italic" Grid.Row="2" HorizontalAlignment="Center"/>
<ComboBox IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial" Grid.Row="2" Grid.Column="1">
<System:String>115200</System:String>
<System:String>57600</System:String>
<System:String>38400</System:String>
<System:String>19200</System:String>
<System:String>14400</System:String>
<System:String>9600</System:String>
<System:String>4800</System:String>
</ComboBox>
<TextBlock Text="Parity :" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center"/>
<ComboBox Grid.Row="2" Grid.Column="3"/>
</Grid>
Pay special attention to use of Grid.Row and Grid.Column, you will need to set the Horizontal Alignment on these elements but this will show you how to position things in WPF.
I have a XAML window that the layout is done using a main grid. The problem is when the browser is resized the controls in the second and third columns move to the right and the window's layout is no longer correct. I have pasted the XAML below. I think the layout of a WPF window should be grid based with all controls residing in the window's master grid. Is this the proper layout design for WPF or is there a different recommended layout pattern?
<Window x:Class="MasterPage.Intake1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:MasterPage.View"
Title="Intake 1" Height="900" Width="1000" Background="#FFD9DDE8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35*"/>
<RowDefinition Height="45*"/>
<RowDefinition Height="190*"/>
<RowDefinition Height="275*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="500*"/>
<ColumnDefinition Width="300*"/>
<ColumnDefinition Width="250*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0">
<Image Source="Images\header.jpg" Height="47" Margin="0,0,0,0" />
</StackPanel>
<StackPanel HorizontalAlignment="Left" Grid.Column="0" Height="80" Width="350" Grid.ColumnSpan="1" Grid.Row="1" Margin="0,-23,0,0">
<Image Source="Images\DSPASS_logo.png" Stretch="Fill" DockPanel.Dock="Top" Height="70" />
</StackPanel>
<StackPanel Grid.Column="4" Grid.Row="1" Grid.ColumnSpan="1" Margin="0,0,0,0">
<TextBlock FontSize="15" Margin="0,0,0,0" Height="25" Width="300" TextWrapping="WrapWithOverflow">
<Label FontSize="10" Name="Namelabel" Width="250" FontWeight="Bold" Foreground="#1664A1" Content="Welcome to DS PASS:"/>
</TextBlock>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="10,10,0,0" Height="290" Width="975" VerticalAlignment="Top" Grid.ColumnSpan="3" Grid.RowSpan="1">
<Label Height="36" FontSize="16" Content="Post Identification" Background="#FF014E7E" FontWeight="Bold" Foreground="White" Margin="-5,0,-18,0"/>
<StackPanel x:Name="_wordLength" Orientation="Horizontal" Margin="235,0,200,0">
<TextBlock Height="20" x:Name="ApplicantType_TextBlock" Text="Applicant Type" Width="110" Margin="5" TextAlignment="Right"/>
<ComboBox Height="25" x:Name="textBoxWordLength" Width="400" Margin="10,5"/>
</StackPanel>
<StackPanel x:Name="_integerWordLength" Orientation="Horizontal" Margin="235,0,200,0">
<TextBlock Height="20" x:Name="textBlockIntegerWordLength" Text="Person Type" Width="110" Margin="5" TextAlignment="Right"/>
<ComboBox Height="25" x:Name="textBoxIntegerWordLength" Width="400" Margin="10,5"/>
</StackPanel>
<StackPanel x:Name="_Post" Orientation="Horizontal" Margin="235,0,200,20">
<TextBlock Height="20" x:Name="Post_TextBlock" Text="Post" Width="110" Margin="5" TextAlignment="Right"/>
<ComboBox Height="25" x:Name="Post_TextBox" Width="400" Margin="10,5"/>
</StackPanel>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Height="130" Margin="15,-120,0,0" VerticalAlignment="Top" Grid.ColumnSpan="3" >
<Label Height="36" FontSize="16" Content="Applicant Employment" Background="#FF004A78" FontWeight="Bold" Foreground="White" Margin="-5,0,-18,0"/>
<StackPanel x:Name="_max1" Orientation="Vertical" Margin="1,0,355,0">
<TextBlock Height="20" x:Name="maxTextBlock1" Text="Employee ID" Width="100" Foreground="Black" Margin="0,15,459,5" TextAlignment="Right"/>
<TextBox Height="25" x:Name="maxTextBox1" Width="400" Margin="0,-5,90,25"/>
</StackPanel>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="3" Orientation="Vertical" HorizontalAlignment="Left" Height="330" Margin="0,10,0,0" VerticalAlignment="Top" Width="965" Grid.RowSpan="2">
<Label Height="36" FontSize="16" Content="English Version of Name" Background="#FF005183" Grid.Row="4" FontWeight="Bold" Margin="10,10,0,0" Foreground="White" Padding="4,5,5,5"/>
<StackPanel x:Name="_LastName" Orientation="Vertical">
<TextBlock Height="20" x:Name="LastName_TextBlock" Text="Last Name/Surname" Margin="55,5,459,5" TextAlignment="Left"/>
<TextBox Height="25" x:Name="LastName_TextBox" Width="500" Margin="55,5,459,5"/>
</StackPanel>
<StackPanel x:Name="_FirstName" Orientation="Vertical">
<TextBlock Height="20" x:Name="FirstName_TextBlock" Text="First Name" Margin="55,0,459,0" TextAlignment="Left"/>
<TextBox Height="25" x:Name="FirstName_TextBox" Width="500" Margin="55,0,459,0"/>
</StackPanel>
<StackPanel x:Name="_MiddleName" Orientation="Vertical">
<TextBlock Height="20" x:Name="MiddleName_TextBlock" Text="Middle Name" Margin="55,5,459,5" TextAlignment="Left"/>
<TextBox Height="25" x:Name="MiddleName_TextBox" Width="500" Margin="55,5,459,5"/>
</StackPanel>
<StackPanel x:Name="_ExtendedName" Orientation="Vertical">
<TextBlock Height="20" x:Name="ExtendedName_TextBlock" Text="Extended name (Tribal, Jr., III, etc.)" Margin="55,5,372,5" TextAlignment="Left"/>
<TextBox Height="25" x:Name="Extended_TextBox" Width="500" Margin="55,5,459,5"/>
</StackPanel>
</StackPanel>
<StackPanel Grid.RowSpan="4" Grid.Column="4" HorizontalAlignment="Left" Height="230" Margin="-250,400,0,0" Width="480" Grid.ColumnSpan="1">
<StackPanel x:Name="_Aliases" Orientation="Vertical" Margin="-15,0,0,0" Height="144">
<TextBlock Height="20" x:Name="Aliases_TextBlock" Text="Aliases" Margin="40,-5,0,5" TextAlignment="Left"/>
<TextBox Height="25" x:Name="Aliases_TextBox" Width="500" Margin="43,5"/>
<TextBox Height="25" x:Name="Aliases_TextBox1" Width="500" Margin="43,5"/>
<TextBox Height="25" x:Name="Aliases_TextBox2" Width="500" Margin="43,5"/>
</StackPanel>
</StackPanel>
<!--<Label HorizontalAlignment="Left" Margin="4,132,0,0" Grid.Row="3" VerticalAlignment="Top" Background="#FF72C772" Grid.ColumnSpan="3" Width="1242" Height="36"/>
<Button Content="Continue" Grid.Column="2" HorizontalAlignment="Left" Margin="16,139,0,0" Grid.Row="3" VerticalAlignment="Top" Width="125" Click="Page1_Continue_Click"/>
<Button Content="Save Draft" Grid.Column="1" HorizontalAlignment="Left" Margin="336,139,0,0" Grid.Row="3" VerticalAlignment="Top" Width="125"/>-->
<Label HorizontalAlignment="Left" Margin="4,400,0,0" Grid.Row="4" VerticalAlignment="Top" Background="#FF52BB52" Grid.ColumnSpan="3" Width="983" Height="36">
</Label>
<Button Content="Save Draft" Grid.Column="1" HorizontalAlignment="Left" Margin="150,408,0,0" Grid.Row="5" VerticalAlignment="Top" Width="125" />
<Button x:Name="ContinueButton_Page2" Content="Continue" Grid.Column="1" Width="125" Grid.Row="5" VerticalAlignment="Top" Margin="200,408,0,0" Grid.ColumnSpan="2" Click="btnContinue_Click"/>
</Grid>
</Window>
When you want control of the alignment during resizing, think Grid. In more complex situations, think Grids inside a Grid. A picture says it better
You have your MainGrid with only rows, no columns. The blue headers get rows of their own and
the orange containers are Grids themselves (with possibly a margin left and right).
Now you shouldn't try to divide the available space with all "...*" width values.
In this example you could use it for dead space only, e.g. the yellow columns with Width="*".
The blue columns would be Width="Auto" (or pixels).
Topping it off with a MinWidth for the Window corresponding with the width you need for the bottom Grid. Also consider wrapping your MainGrid in a ScrollViewer.
What I needed was to not allow resizing of the web browser. To do this I removed the minimize/maximize buttons To do that I used the following attribute:
ResizeMode="NoResize"
In the example below I cannot get the ScrollViewer to fill the space available, The height is unknown due to the dynamic content above but is there no way it can fill the space available instead of over running?
<Grid x:Name="Main" Height="200" MaxHeight="200">
<StackPanel>
<Grid x:Name="MainContent" Height="170" MaxHeight="170">
<StackPanel>
<TextBlock FontSize="24" Text="Dynamic data "/>
<TextBlock FontSize="24" Text="height "/>
<TextBlock FontSize="24" Text="unknown... "/>
<Grid x:Name="Results" Background="Red">
<ScrollViewer>
<StackPanel>
<TextBlock FontSize="24" Text="Result set... 0"/>
<TextBlock FontSize="24" Text="Result set... 1"/>
<TextBlock FontSize="24" Text="Result set... 2"/>
<TextBlock FontSize="24" Text="Result set... 3"/>
</StackPanel>
</ScrollViewer>
</Grid>
</StackPanel>
</Grid>
<Grid x:Name="Nav">
<Button HorizontalAlignment="Left" Content="Back"/>
<Button HorizontalAlignment="Right" Content="Forward"/>
</Grid>
</StackPanel>
</Grid>
in MainContent Grid use DockPanel instead of StackPanel with LastChildFill=True, something like this:
<Grid x:Name="MainContent" Height="170" MaxHeight="170">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Top" FontSize="24" Text="Dynamic data "/>
<TextBlock DockPanel.Dock="Top" FontSize="24" Text="height "/>
<TextBlock DockPanel.Dock="Top" FontSize="24" Text="unknown... "/>
<Grid x:Name="Results" Background="Red">
<ScrollViewer>
<StackPanel>
<TextBlock FontSize="24" Text="Result set... 0"/>
<TextBlock FontSize="24" Text="Result set... 1"/>
<TextBlock FontSize="24" Text="Result set... 2"/>
<TextBlock FontSize="24" Text="Result set... 3"/>
</StackPanel>
</ScrollViewer>
</Grid>
</DockPanel>
</Grid>
then last element of DockPanel will adjust itself to available space
I solved it by adding another element for the Scrollviewer's Height to bind:
<grid Grid.row="3" x:Name="Mirror">
</grid>
<ScrollViewer Grid.row="3" Height="{Binding ElementName=uxMirror,XPath=ActualHeight}">
</ScrollViewer>
Firstly your layout is kind of a mess. Why do you need to put a StackPanel as the only child of a Grid. For the Rows? Use RowDefinitions and ColumnDefinitions when dealing with Grid
From my understanding of your layout, you can pretty much do everything you need with just a Grid and a DockPanel. When dealing with layout containers, Aim to get your layout with the "least" number of containers as "efficiently" possible. Even if it aint any real performance benefit, it helps enormously when going through someone else's code to not have redundant nesting
<Grid x:Name="Main">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
FontSize="24"
Text="Dynamic data " />
<TextBlock Grid.Row="1"
FontSize="24"
Text="height " />
<TextBlock Grid.Row="2"
FontSize="24"
Text="unknown... " />
<ScrollViewer Grid.Row="3"
Background="Red">
<StackPanel>
<TextBlock FontSize="24"
Text="Result set... 0" />
<TextBlock FontSize="24"
Text="Result set... 1" />
<TextBlock FontSize="24"
Text="Result set... 2" />
<TextBlock FontSize="24"
Text="Result set... 3" />
</StackPanel>
</ScrollViewer>
<DockPanel x:Name="Nav"
Grid.Row="4"
LastChildFill="False">
<Button Content="Back"
DockPanel.Dock="Left" />
<Button Content="Forward"
DockPanel.Dock="Right" />
</DockPanel>
</Grid>
This should give you everything your looking for. If you need to restrict Height dimensions with static values, Add them in accordingly when absolutely needed.
As for the "Nav" DockPanel, yes you can using HorizontalAlignment get your Button's positioned Left and Right and thereby not use the DockPanel, but that would go against the concept of trying to keep "one" item in a Grid cell and hence the DockPanel usage.
I've been working on how to do layouts and I've come across something that is bugging the heck out of me. Now, if I use the code below, the window takes up the entirety of my screen.
<Window x:Class="HDD_Drill_View.Windows.WndwMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="HDDC"
WindowStartupLocation="CenterScreen"
Name="wndwMain"
Closing="WindowClosing"
ResizeMode="NoResize"
WindowState="Normal"
SizeToContent="WidthAndHeight">
<Grid>
<DockPanel Width="Auto"
LastChildFill="False"
HorizontalAlignment="Left">
<DockPanel.Background>
<ImageBrush ImageSource="..\Resources\background.png"
Stretch="UniformToFill"
TileMode="None" />
</DockPanel.Background>
<StackPanel DockPanel.Dock="Top"
Height="Auto"
Width="Auto"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Button HorizontalAlignment="Left"
Name="bttnGenerateReports"
VerticalAlignment="Top"
UseLayoutRounding="False"
Click="BttnGenerateReportsClick"
Margin="10">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"><Run Text="Generate" /><LineBreak /><Run Text=" Reports" /></TextBlock>
</Button>
<Button HorizontalAlignment="Left"
Name="bttnSurveyReport"
VerticalAlignment="Top"
UseLayoutRounding="False"
Click="BttnSurveyReportClick"
Margin="10">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"><Run Text="Survey" /><LineBreak /><Run Text="Report" /></TextBlock>
</Button>
<Button HorizontalAlignment="Left"
Name="bttnTimeChart"
VerticalAlignment="Top"
UseLayoutRounding="False"
Click="BttnTimeChartClick"
Margin="10">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"><Run Text=" Time" /><LineBreak /><Run Text=" Chart" /></TextBlock>
</Button>
<Button HorizontalAlignment="Left"
Name="bttnMaterialAcquisition"
VerticalAlignment="Top"
UseLayoutRounding="False"
Click="BttnMaterialAcquisitionClick"
Margin="10">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"><Run Text=" Material" /><LineBreak /><Run Text="Acquistion" /></TextBlock>
</Button>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom"
Height="Auto"
Width="Auto"
Orientation="Horizontal">
<!--MaxWidth="800"
MaxHeight="80"-->
<StackPanel.Background>
<ImageBrush ImageSource="..\Resources\drildata2.jpg" />
</StackPanel.Background>
</StackPanel>
</DockPanel>
</Grid>
</Window>
Yet, if I uncomment the Max Height and Max Width in the last StackPanel, then it seems fine. Basically, I'm wanting my background image to NOT affect my window size. Is this possible? I'm trying to set the background of a StackPanel to an image.
Remove the SizeToContent="WidthAndHeight" from the Window.
Please help me to edit XAML so the TextBlock which shows minutes goes to the right top corner.
<StackPanel Orientation="Horizontal" >
<StackPanel Orientation="Horizontal" >
<TextBlock Name="UserNameTextBlock" Margin="0,0,8,0" VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
<TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >#</TextBlock>
<TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock" Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
<TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo" Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>
</StackPanel>
</StackPanel>
So it should be like
I would use a DockPanel. For the child nodes, just add DockPanel.Dock attributes to indicate where you want the element to go. The last child element will automatically fill the remaining area.
<DockPanel>
<TextBlock DockPanel.Dock="Right" FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo" Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>
<StackPanel Orientation="Horizontal" >
<TextBlock Name="UserNameTextBlock" Margin="0,0,8,0" VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
<TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >#</TextBlock>
<TextBlock FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock" Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
</StackPanel>
</DockPanel>
If I were doing this I would use a grid rather than a horizontal stack panel:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Name="UserNameTextBlock" Margin="0,0,8,0" VerticalAlignment="Bottom" FontSize="15" Text="{Binding Path=UserName}" FontWeight="Bold"></TextBlock>
<TextBlock Grid.Column="1" FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Foreground="LightGray" >#</TextBlock>
<TextBlock Grid.Column="2" FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="ScreenNameTextBlock" Text="{Binding Path=ScreenName}" Foreground="Gray" ></TextBlock>
<TextBlock Grid.Column="4" FontSize="13" VerticalAlignment="Bottom" Padding="0,0,0,1" Name="MinAgo" Text="{Binding Path=MinAgo}" Foreground="Gray" ></TextBlock>
</Grid>
Note the * in the column 3 which means that column will use all available space after column 2, except for what is needed by column 4.
Don't use a StackPanel but a Grid with columns?