TextBlock text wrapping pushes other components - wpf

I have a TextBlock and a couple Buttons in a StatusBar. The Buttons are effectively right-aligned (e: actually the StatusBarItem containing them is). When the window is shrunk horizontally, and the text wraps, the TextBlock pushes the Buttons off the window to varying degrees.
Why does this happen, and how can I fix the positions of the Buttons?
<Window x:Class="TextWrapping.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem>
<TextBlock Name="statusText" TextWrapping="Wrap" Text="when this text wraps, it pushes the buttons off the window" />
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal">
<Button>one</Button>
<Button>two</Button>
</StackPanel>
</StatusBarItem>
</StatusBar>
<TextBlock Text="shink this window horizontally" />
</DockPanel>
</Window>

You can see this blog post for more information, but basically the StatusBar is using a DockPanel to present it's items. So for the code you have above, the statusText is being docked left, and the buttons are filling the remaining space (but aligned horizontally within that area).
So as you size smaller, the TextBlock will always take as much space as it needs (allowing the buttons to size to zero). When the text is wrappped, the buttons get back a little more space as the TextBlock doesn't need all the horizontal space.
To fix it you can change your code to:
<DockPanel>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem DockPanel.Dock="Right">
<StackPanel Orientation="Horizontal">
<Button>one</Button>
<Button>two</Button>
</StackPanel>
</StatusBarItem>
<StatusBarItem>
<TextBlock Name="statusText" TextWrapping="Wrap" Text="when this text wraps, it pushes the buttons off the window" />
</StatusBarItem>
</StatusBar>
<TextBlock Text="shink this window horizontally" />
</DockPanel>
Or you can use the trick shown the blog post, to replace the DockPanel with a Grid.

Related

StackPanel in StatusBar won't stretch

When I add StackPanel to the StatusBar as last StatusBarItem it won't fill up all additional space. I wonder why is that since StatusBar internally uses DockPanel as ItemsPanel.
Here's a sample code:
<Window x:Class="Foo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Zadatak1"
Title="MainWindow"
WindowState="Maximized">
<DockPanel>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem DockPanel.Dock="Left" Background="SkyBlue">
<Label>Status....</Label>
</StatusBarItem>
<StatusBarItem Background="Red" DockPanel.Dock="Right" HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal">
<Label>Time:</Label>
<Label>02:48:AM</Label>
</StackPanel>
</StatusBarItem>
</StatusBar>
</DockPanel>
If I change the order of the elements so Label is first and StackPanel is second, it fills additional space as it should.
It is the red StatusBarItem that doesn't fill the space since you set its HorizontalAlignment property to Right. Change it to the default value of Stretch if you want it to fill the DockPanel.
The StackPanel does fill the StatusBarItem as expected.

How to control button layout in Xaml

Im having trouble controlling the exact layout of a button control with XAML.
It seems that whatever i do the button is of a minimum width.
I have a simple button with only a textblock inside the button. But the button has a lot of margin and padding that i cant seem to get rid of (i know of negative margins and padding).
The things i want to know is:
1. Why in the world was it designed this way.
2. what are the groundrules for controlling the exact layout of a button?
My code is as follows:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"></StackPanel>
<Pivot Grid.Row="1">
<Pivot.Title>
<StackPanel Orientation="Horizontal" Margin="-15,-3,0,0" Background="red" Width="480">
<Button Background="Blue" x:Name="btnStudies" Click="btnMenuItem_Click" Width="20">
<TextBlock Text="Title" Foreground="White"></TextBlock>
</Button>
<Button Background="Green">
<TextBlock Text="Title" Foreground="White"></TextBlock>
</Button>
<Button Background="Blue" Click="btnMenuItem_Click">
<TextBlock Text="Title" Foreground="White"></TextBlock>
</Button>
<Button Background="Blue" Click="btnMenuItem_Click">
<TextBlock Text="Title" Foreground="White"></TextBlock>
</Button>
<Button Background="Blue" Click="btnMenuItem_Click">
<TextBlock Text="Title" Foreground="White"></TextBlock>
</Button>
</StackPanel>
</Pivot.Title>
</Pivot>
</Grid>
I want five buttons in a row but these are already too wide for the screen (windows phone). Changing the width doesnt seem to have any effect (why is it there).
The textBlock control within the button the button is as wide as the text on it, but i dont seem to have any control on the width of the button. In HTML you only have padding or margin when you define it but in xaml it just seems to be there and for me its unclear how to undo that.
*****EDIT*****
After reading Rachel's reply i decided to start from the ground up.
Using the code below i still have no control over how wide the button is because it uses a certain amount of padding that i cant seem to remove. The button has a width of about 110 when i define a width lower than that it doesnt change. Margins and paddings of 0 have no effect at all (dont want to use negative values just yet because that doesnt seem very intuitive). So the code below is very simple but still the button takes up an amount of space that i dont have any control over. I cant imagine a reason why it was designed this way.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400" />
</Grid.ColumnDefinitions>
<StackPanel Width="300" Background="Red" HorizontalAlignment="Left">
<Button Background="Blue" HorizontalAlignment="Left" Width="100" Margin="0" Padding="0">
<TextBlock Text="Title" Width="Auto" HorizontalAlignment="Left" />
</Button>
</StackPanel>
</Grid>
The type and size of the parent panel containing the control affects the size/layout of the child controls.
In your case, you have a Grid as your parent panel, and a Grid defaults to taking up all available space. In addition, children placed inside the grid default to taking up all available space as well unless you specify otherwise.
So your <Pivot> is being assigned a width equal to Grid.Width, and Pivot.Title sounds like it's being assigned a width equal to Pivot.Width, and StackPanel is being assigned a width equal to Pivot.Title.Width... you get the picture.
To specify that a control should not take up all available space, specify a HorizontalAlignment or VerticalAlignment property to tell it what side of the parent panel to dock the item on.
For example
<Pivot Grid.Row="1" HorizontalAlignment="Left">
or
<StackPanel OWidth="480" HorizontalAlignment="Left" ...>
If you're new to WPF's layout system, I would recommend reading through the codeproject article WPF Layouts: A Quick Visual Start to quickly learn what the main layout panels are for WPF.

WPF DockPanel not Docking / Default fixed width and minimum width

I'm trying to make a footer control that has a minimum height that it will shrink to before allowing the a window resize encroaches on its view-able area. But I want a fixed width that it will adhere to until a resize of the main window encroaches on its default bounds. Given the following code, what I'm not understanding is:
-On shrinking the window size after running the sample, why is the bottom anchor of the lower canvas not respected? Instead it's anchoring to the canvas above it.
-Why is the is minimum size of the bottom panel not shrunk too before the window encroaches on its area?
-Why do I have to add the bottom canvas before the top for this demo to even layout correctly?
-Lastly, is there a way to make a Window's minimum bounds just be the sum of all the horizontal and vertical minimum bounds of the controls it contains?
<Window x:Class="TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="1278" Background="{x:Null}">
<DockPanel Background="#FFE6AFAF" VerticalAlignment="Stretch" >
<Canvas DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" MinHeight="100" Height="170" Margin="10,10,10,10" VerticalAlignment="Stretch" Width="Auto">
<Canvas.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>
</Canvas.Background>
</Canvas>
<Canvas DockPanel.Dock="Top,Bottom" HorizontalAlignment="Stretch" Height="Auto" Margin="10,10,10,10" VerticalAlignment="Stretch" Width="Auto">
<Canvas.Background>
<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}"/>
</Canvas.Background>
</Canvas>
</DockPanel>
</Window>
To start with question 3:
When using a DockPanel, the order in which you add elements matters. What happens here is that the "MinHeight=100" canvas is added first, and the DockPanel.Dock="Bottom" says: "Stretch this canvas across the bottom of the DockPanel and make it as tall as it needs to be, but no taller, because we need to keep as much space as possible available for the rest of the elements".
This process is then repeated for each consecutive element in the DockPanel until the very last element, which gets to use all the space that is left in the DockPanel (unless you set <DockPanel LastChildFill="False" ...). This example might help illustrate how the DockPanel works:
<DockPanel Width="200" Height="200" >
<Button Content="01" Background="#222" DockPanel.Dock="Bottom" />
<Button Content="02" Background="#333" DockPanel.Dock="Left" />
<Button Content="03" Background="#444" DockPanel.Dock="Top" />
<Button Content="04" Background="#555" DockPanel.Dock="Right" />
<Button Content="05" Background="#666" DockPanel.Dock="Bottom" />
<Button Content="06" Background="#777" DockPanel.Dock="Left" />
<Button Content="07" Background="#888" DockPanel.Dock="Top" />
<Button Content="08" Background="#999" DockPanel.Dock="Right" />
<Button Content="09" Background="#aaa" DockPanel.Dock="Bottom" />
<Button Content="10" Background="#bbb" DockPanel.Dock="Left" />
<Button Content="11" Background="#ccc" DockPanel.Dock="Top" />
<Button Content="12" Background="#ddd" />
</DockPanel>
So in your case, the first canvas is anchored to the bottom and stretches horizontally across the DockPanel. Its height will always be 100 pixels, because its MinHeight says that's the lowest height it will accept.
Then, the second canvas is added, and because it's the last element, it's allowed to use all the space that's left above the first canvas.
Question 3, part "-Lastly":
Try <Window ... SizeToContent="WidthAndHeight" />
Question 2:
You mean if you shrink the window to be less than 100 pixels tall? Elements will never accept to be smaller than their minimum size (in this case 100 pixels tall). The canvas renders itself at 100 pixels, and what doesn't fit inside the window simply gets clipped.
..and I'm not sure what you mean in Question 1..

TextBox expanding with surrounding Grid but not with text

A window has a Grid with two columns. The left column contains a control with a constant width but with a height that adapts. The right column contains a TextBox that takes up all remaining space in the Grid (and thereby in the Window).
The Grid is given a minimal width and height and is wrapped within a ScrollViewer. If the user resizes the window to be smaller than the minimal width/height of the Grid, scrollbars are displayed.
This is exactly how I want it to be. However, a problem occurs when the user starts typing text. If the text is to long to fit in one line in the TextBox, I want the text to wrap. Therefore I set TextWrapping="Wrap" on the TextBox. But since the TextBox has an automatic width and is wrapped in a ScrollViewer (its actually the whole Grid that is wrapped), the TextBox just keeps expanding to the right.
I do want the TextBox to expand if the window is expanded, but I don't want the TextBox to expand by the text. Rather the text should wrap inside the available TextBox. If the text don't fit within the TextBox height, a scrollbar should be displayed within the TextBox.
Is there a way to accomplish this?
Below is some code that shows my problem:
<Window x:Class="AdaptingTextBoxes.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400" Background="DarkCyan">
<Grid Margin="10" Name="LayoutRoot">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid MinWidth="300" MinHeight="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="0,0,10,0" Content="Button" Width="100" />
<TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
</Grid>
</ScrollViewer>
</Grid>
</Window>
You could use an invisible border (its hacky but it works - its how I tend to sort out dynamic textbox sizes in Xaml):
<Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
<TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
Have you tried setting the MaxWidth property on just the TextBox?
Edit after OP's comment
I would try getting rid of the ScrollViewer. The sizing used in the Grid's layout should take care of re-sizing and the scroll bar settings on the TextBox should take care of the rest.
The answer is based on Leom's answer.
The solution works great when you enlarge the window, but the resizing is not smooth when you make the window smaller. As the textbox participates in the grid's layout, it has to perform layout process multiple times. You can fix that by putting the texbox in the canvas, so the change of the size of the textbox no longer triggers the grid's re-layout.
The updated code:
<Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
<Canvas Grid.Column="1">
<TextBox AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
</Canvas>

How to incorporate Canvas into a larger layout in WPF?

Canvas doesn't seem to play well together nicely with the other elements when you try to build it into a layout and have e.g. controls on the side and the canvas is the drawing area.
For instance, why can I put a border around every element except a canvas? In the following code, border wraps canvas but the canvas only has the border on the top but not on the left, right or bottom:
<Window x:Class="WpfApplication25.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock DockPanel.Dock="Bottom" Text="Move the slider to reveal the answer:"/>
<Slider DockPanel.Dock="Bottom" Name="theSlider"
HorizontalAlignment="Left"
Width="200"
Minimum="0"
Maximum="1"
Value="1"
Cursor="Hand"/>
<Border BorderBrush="Tan" BorderThickness="2">
<Canvas>
<TextBlock Canvas.Left="45" Canvas.Top="50" Text="test" FontSize="16"/>
<Rectangle
Canvas.Left="10"
Canvas.Top="10"
Width="100"
Height="100"
Fill="Silver"
Opacity="{Binding ElementName=theSlider, Path=Value}"
/>
</Canvas>
</Border>
</StackPanel>
</Window>
From what I can tell in XamlPad, the problem appears to be that your Canvas does not have an explicit height/width, and that its HorizontalAlignment defaults to being in the middle of the Border. Without an explicit height and width the Border appears to collapse to 0 height and stretches on the width. My assumption is this is because your Border is in a StackPanel, as placing the Border in a Grid, causes it to behave as expected.
Your best bet is to give the Canvas an explicit Height and Width. Not sure that is what you're looking for though.
As far as I understand what you are trying to achieve, you should place your controls in one cell of a Grid and your Canvas in another.

Resources