DockPanel.Dock = "Right" does not work for one control in the "Maximum" window?

I use DockPanel.Dock to control the docking in a specific place (i.e. left / right). The problem is that my controls do not dock according to the position of the DockPanel.Dock.

Below is the code for one control with DockPanel.Dock="Right"

  <DockPanel> <TextBlock Text ="Left1" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Left2" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Right1" Margin ="5" DockPanel.Dock ="Right" Style ="{StaticResource TextBlockStyle}" /> </DockPanel> 

enter image description here code for multiple controls with DockPanel.Dock="Right"

  <DockPanel> <TextBlock Text ="Left1" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Left2" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Right1" Margin ="5" DockPanel.Dock ="Right" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Right2" Margin ="5" DockPanel.Dock ="Right" Style ="{StaticResource TextBlockStyle}" /> </DockPanel> 

enter image description here

Waiting for withdrawal:

enter image description here

Any idea or thought will be appreciated. Thanks at Advance

+4
source share
3 answers

You should use the LastChildFill property:

 <DockPanel LastChildFill="False"> <TextBlock Text ="Left1" Margin ="5" DockPanel.Dock ="Left" /> <TextBlock Text ="Left2" Margin ="5" DockPanel.Dock ="Left" /> <StackPanel Orientation="Horizontal" DockPanel.Dock="Right"> <TextBlock Text ="Right1" Margin ="5" /> <TextBlock Text ="Right2" Margin ="5" /> </StackPanel> </DockPanel> 
+13
source

This is because the LastChildFill DockPanel property is LastChildFill DockPanel by default. For the desired output, set it to false .

By MSDN :

If you set the LastChildFill property to true, which is the default setting, the last DockPanel child always fills the remaining space, regardless of any other dock value that you set on the last child. To prove a child in a different direction, you must set the LastChildFill property to false and specify the explicit direction of the dock for the last child.

Example UI and XAML using DockPanel:

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="DockPanel Sample"> <DockPanel LastChildFill="True"> <Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top"> <TextBlock Foreground="Black">Dock = "Top"</TextBlock> </Border> <Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Top"> <TextBlock Foreground="Black">Dock = "Top"</TextBlock> </Border> <Border Height="25" Background="LemonChiffon" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Bottom"> <TextBlock Foreground="Black">Dock = "Bottom"</TextBlock> </Border> <Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1" DockPanel.Dock="Left"> <TextBlock Foreground="Black">Dock = "Left"</TextBlock> </Border> <Border Background="White" BorderBrush="Black" BorderThickness="1"> <TextBlock Foreground="Black">This content will "Fill" the remaining space</TextBlock> </Border> </DockPanel> </Page> 

XAML Sample

If you do not want to exceed the behavior, set LastChildFill = "False" in the example above XAML and see the result.

+8
source

Add HorizontalAlignment = "Right" as below

  <TextBlock Text ="Left1" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Left2" Margin ="5" DockPanel.Dock ="Left" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Right1" Margin ="5" DockPanel.Dock ="Right" HorizontalAlignment="Right" Style ="{StaticResource TextBlockStyle}" /> <TextBlock Text ="Right2" Margin ="5" DockPanel.Dock ="Right" HorizontalAlignment="Right" Style ="{StaticResource TextBlockStyle}" /> 

+2
source

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


All Articles