Why TextBlock is not sizing to its content? - wpf

Why is the following TextBlock not sizing to its content? Its width (as shown with blue backgroud) is covering the width of the entire window.
What am I missing here and how can I fix it?
I have set Width="Auto" for the textblock. NOTE: I don't want to set the entire window to SizeToContent="WidthAndHeight" since it would have other controls that we don't need to use this setting for. This is just a minimal demo for this post only:
<Page x:Class="WPTEST.HomePage"
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:WPF_TEST"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="HomePage">
<Grid>
<StackPanel VerticalAlignment="Center">
<TextBlock Width="Auto" FontSize="16" Background="Blue" Foreground="White" FontWeight="Bold" Text="Welcome To Home Page. Your home page to perform main tasks"/>
</StackPanel>
</Grid>
</Page>
MainWindow Display:

Set HorizontalAlignment="Left". The default value is "Stretch", which means that the control should be stretched to fill the space available in the containing element. Setting to "Left" will disable that behavior and allow the TextBlock to grow only as much needed to fit the text:
<Grid>
<StackPanel VerticalAlignment="Center">
<TextBlock HorizontalAlignment="Left"
FontSize="16" Background="Blue" Foreground="White" FontWeight="Bold"
Text="Welcome To Home Page. Your home page to perform main tasks"/>
</StackPanel>
</Grid>

Related

Change the height of the title bar

Question: Can we change the height of the title bar displayed in MahApps.Metro?
Details: For instance, in the following XAML example from the MahApps team, I want to display the content Deploy CupCake - of the TextBlock - below the image of the CupCake. So I removed the Orientation="Horizontal" from the StackPanel in the following XAML. As shown in the snapshot below, the content Deploy CupCake is now showing below the image of CupCake - but it is hiding almost all of it. How can we make this content show all of it below the CupCake image?
Snapshot of the Toolbar with MahApps.Metro: Only about 10% of the content is showing below the image.
<mah:MetroWindow x:Class="SampleApp.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:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<mah:MetroWindow.LeftWindowCommands>
<mah:WindowCommands>
<Button Click="LaunchGitHubSite" ToolTip="Open up the GitHub site">
<iconPacks:PackIconModern Width="22"
Height="22"
Kind="SocialGithubOctocat" />
</Button>
</mah:WindowCommands>
</mah:MetroWindow.LeftWindowCommands>
<mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands>
<Button Click="DeployCupCakes" Content="Deploy CupCakes">
<Button.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconModern Width="22"
Height="22"
VerticalAlignment="Center"
Kind="FoodCupcake" />
<TextBlock Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>
<Grid>
<!-- Your content -->
</Grid>
</mah:MetroWindow>
First of all, it is better to use a to use a panel that distributes the available space among its content, like a Grid, to prevent cutting off content as with the StackPanel. Here, the data template defines a Grid with two rows, where the TextBlock scales to its desired size and the icon takes up the remaining available space. Also note the HorizontalAlignment of the icon, it is centered.
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<iconPacks:PackIconModern Grid.Row="0"
Width="22"
Height="22"
HorizontalAlignment="Center"
Kind="FoodCupcake" />
<TextBlock Grid.Row="1"
Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
</Grid>
</DataTemplate>
The same result could also be achieved using a DockPanel, where the last child (in this case the icon) takes up the remaining available space, so the order of the defined controls is important.
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Bottom"
Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
<iconPacks:PackIconModern DockPanel.Dock="Top"
Width="22"
Height="22"
HorizontalAlignment="Center"
Kind="FoodCupcake" />
</DockPanel>
</DataTemplate>
In both cases you will get the result below, a button with a centered icon and a text below.
To make the button more prominent, change the title bar height with the TitleBarHeight property.
<mah:MetroWindow x:Class="SampleApp.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:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
TitleBarHeight="50">

How can I automatically resize objects that are in a grid?

So I have recently started learning more C# and I am currently trying to create a GUI. I so far have a title and a text box that looks like this:
https://i.imgur.com/BvpkQ3U.png
Yes, this looks fine, it's not the problem. The problem is when you open the window (by debugging it) the window is set to the size of which it looks like on the preview. Which is fine. But, when I maximize it the objects inside of the window do not get larger. They stay at the same size. Which looks like this:
BEFORE:
https://i.imgur.com/NuGTDwf.png
AFTER:
https://i.imgur.com/pVy2aYk.png
How can I make it so the objects inside of the window get larger just like the window itself does?
MainWindow.xaml:
<Window x:Name="THE_GUI" x:Class="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:GUI"
mc:Ignorable="d"
Title="GUI" Height="450" Width="800" Foreground="Black" WindowStyle="SingleBorderWindow">
<Grid>
<TextBlock x:Name="GUI_TITLE2" HorizontalAlignment="Center" Margin="0,10,0,0" Text="TITLE" TextWrapping="Wrap" VerticalAlignment="Top" Height="50" Width="125" FontSize="18"/>
<TextBox HorizontalAlignment="Left" Margin="41,35,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="204" Height="375" IsReadOnly="True"/>
</Grid>
</Window>
If you require more code I can provide it
Thanks! :)
PS
I did see this question: WPF: How can I have controls in a grid automatically resize when the grid is resized? but it doesn't help me at all.
Do not set Height & Width of controls (TextBlock/TextBox) if you want them to resize. Also you are using a plain grid with no Row/Columns so WPF will not know how to scale your GUI.
You need to read about different type of Panels/Containers available in WPF and how to use them to Lay your Controls.
Check: StackPanel, WrapPanel, DockPanel. For Grid read about RowDefinitions and Columndefinitions. Read the WildCard height and width assignment.
As suggested by Prateek also, simply putting controls under grid won't work. If you want to go with grid control, then please have row and columns created to align the controls. I've updated your code and here it is:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="13*"/>
<RowDefinition Height="87*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="1"
x:Name="GUI_TITLE2" HorizontalAlignment="Stretch"
Text="TITLE" TextWrapping="Wrap" VerticalAlignment="Stretch"
FontSize="18"/>
<TextBox Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Stretch" Text="TextBox" TextWrapping="Wrap"
VerticalAlignment="Stretch" IsReadOnly="True"/>
</Grid>

Displaying a large image in ScrollViewer in WPF

I want to display a large image in a Window where the Image will have scroll bars if it too big for the area of the screen it is in.
Underneath the image I want a button panel. For this I have put the Image inside a ScrollViewer in a DockPanel that contains a StackPanel to contain the Buttons in the Bottom part. The idea is to click the Browse button to set the image (from code behind handling Button Click)
The following example I put together will just keep the image size (2144 x 1424) and I cannot see the lower button panel.
<Window x:Class="WpfIssues.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:WpfIssues"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top"></TextBlock>
<StackPanel Orientation="Vertical">
<DockPanel x:Name="PhotoPanel">
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Click="Button_Click">Browse...</Button>
</StackPanel>
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<StackPanel>
<Image x:Name="PhotoImage"
Stretch="None"
Source="Resources/bear grills.png"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</StackPanel>
</ScrollViewer>
</DockPanel>
</StackPanel>
</DockPanel>
</Grid>
</Window>
I cant figure out why.
Try putting the button before the scrollviewer, like this:
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top" />
<Button Content="Browse..." DockPanel.Dock="Bottom" />
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image x:Name="PhotoImage"
Stretch="None"
Source="http://images6.fanpop.com/image/photos/33600000/Bear-Grylls-bear-grylls-33656894-3504-2336.jpg"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ScrollViewer>
</DockPanel>

Frame Page controls layout

Could you please tell me where I have gone wrong, my frame size takes the size of the wpf window, code is:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BSP" Height="700" Width="990" WindowStartupLocation="CenterScreen">
<Grid Background="Green" VerticalAlignment="Center" HorizontalAlignment="Center" >
<Frame Name="MainFrame" Source="LoginScreen.xaml" NavigationUIVisibility="Hidden" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</Window>
The page size less than the window and also the total size of margin+width+size of label is less than the size of page yet the button is being displayed as cut. Code for the page is below:
<Page x:Class="Loggedin"
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"
mc:Ignorable="d"
Height="660" Width="920"
Title="Loggedin">
<Grid Background="white" Margin="2">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
<Label Content="Loggedin as" Height="28" HorizontalAlignment="Left" Name="Loggedinas" />
<Button Content="LogOut" Margin="780,0,0,0" Height="28" Name="Logout" Width="50" HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
</Page>
Could you please assist me, it is eating my brain from 2 days :(. Following are pics of output:
Pic from Visual Studios: http://clip2net.com/s/1Fpb7
Pic from Runtime: http://clip2net.com/s/1FpbQ
Have you thought about using a DockPanel instead of a StackPanel? It seems to be a closer fit for the layout you're trying to achieve :
<DockPanel VerticalAlignment="Top">
<Label Content="Loggedin as" Height="28" HorizontalAlignment="Left" Name="Loggedinas" />
<Button Content="LogOut" Height="28" Name="Logout" Width="50" HorizontalAlignment="Right"/>
</DockPanel>
Any time you have margins that look like Margin="780,0,0,0" it usually means you're working too hard and there's a better way to do it. In my quick scratch tests, the 780 pixel left margin was pushing the button off the frame.

Why this example from Pro Silverlight 4 book doesnt work?

I am reading a Pro Silverlight 4 book (http://my.safaribooksonline.com/book/programming/csharp/9781430229797/xaml/element-to-element_binding), and I do all examples from the book. But an example from the binding chapter doesnt work for me. The slider doesnt move after I compile and run the application:
<UserControl
x:Class="SilverlightApplication14.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid
x:Name="LayoutRoot"
Background="White">
<Slider
x:Name="sliderFontSize"
Margin="3"
Minimum="1"
Maximum="40"
Value="10"></Slider>
<TextBlock
Margin="10"
Text="Simple Text"
x:Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"></TextBlock>
</Grid>
Dave S, is absolutely correct. You can see the Z-Index problem reversing the order of the controls (so the TextBlock is before the Slider)- it will then work because the slider will be on top of the TextBlock:
<Grid
x:Name="LayoutRoot"
Background="White">
<TextBlock
Margin="10"
Text="Simple Text"
x:Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"></TextBlock>
<Slider
x:Name="sliderFontSize"
Margin="3"
Minimum="1"
Maximum="40"
Value="10"></Slider>
</Grid>
You can see here that Z-Index is determined by the order of controls in the XAML.
An alternate way round this (or to demonstrate) is to specify the Z-Index attached property explicitly:
<Grid
x:Name="LayoutRoot"
Background="White">
<Slider
x:Name="sliderFontSize"
Margin="3"
Minimum="1"
Maximum="40"
Value="10" Canvas.ZIndex="1"></Slider>
<TextBlock
Margin="10"
Text="Simple Text"
x:Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"></TextBlock>
</Grid>
The best way to fix this is to simply make sure that the elements don't overlap each other, by putting them in different rows:
<Grid
x:Name="LayoutRoot"
Background="White">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Slider Grid.Row="1"
x:Name="sliderFontSize"
Margin="3"
Minimum="1"
Maximum="40"
Value="10"></Slider>
<TextBlock
Margin="10"
Text="Simple Text"
x:Name="lblSampleText"
FontSize="{Binding ElementName=sliderFontSize, Path=Value}"></TextBlock>
</Grid>
In this sample, the TextBlock is in row 0 and the slider is in row 1, so they no longer overlap.
Both the Slider and the TextBlock are in the Grid in the same position (0,0). This means the TextBlock is being shown drawn directly on top of the Slider so any mouse events will always be captured by the TextBlock and not the Slider. This is implicitly implied as the TextBlock has the higher Z-Index by being defined second in the Grid. If you re-arrange the Grid and apply either Grid.Row="1" or Grid.Column="1" to the TextBlock so it is beside the Slider control you should be able to use the Slider successfully.

Resources