Panel size wpf xaml

I am sure there is a simple explanation of why this is happening, but it seems it cannot find it. From the code below, why are my text fields passing outside the window? I would think, since I set their width to the size of the window that they would fit perfectly ... however, run this and this is obviously not the case. What am I missing? What will be the correct way to set the width?

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="200"> <StackPanel Orientation="Horizontal"> <TextBox Width="100">Hello</TextBox> <TextBox Width="100" TextAlignment="Right">World</TextBox> </StackPanel> </Window> 
+4
source share
1 answer

The width of the window includes elements such as the border, and the height includes elements such as the title bar. Therefore, you need to consider this when setting the width / height. You can use the SizeToContent option in the window so that the size is such that it matches the text fields.

how

 <Window ... SizeToContent="Width" 

Or you can replace your StackPanel with a grid to give each TextBox window width the available window width:

 <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBox Grid.Column="0">Hello</TextBox> <TextBox Grid.Column="1" TextAlignment="Right">World</TextBox> </Grid> 
+2
source

Source: https://habr.com/ru/post/1347823/


All Articles