我使用DockPanel.Dock在特定位置(即左/右)进行对接控制。问题是我的控件没有根据DockPanel.Dock位置停靠。
以下是使用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>
使用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>
期待输出:
任何想法或想法将不胜感激。在此先感谢
答案 0 :(得分:18)
您应该使用LastChildFill
属性:
<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>
答案 1 :(得分:10)
这种情况正在发生,因为LastChildFill
的 DockPanel
属性默认为true
。对于您想要的输出,将其设置为false
。
根据MSDN
:
如果将LastChildFill属性设置为true(默认设置),则DockPanel的最后一个子元素始终填充剩余空间,而不管您在最后一个子元素上设置的任何其他停靠值。要将子元素停靠在另一个方向上,必须将LastChildFill属性设置为false,并且还必须为最后一个子元素指定显式停靠方向。
使用DockPanel的示例UI和XAML:
<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示例中设置LastChildFill =“False”并观察结果。
答案 2 :(得分:2)
如下所述添加HorizontalAlignment =“Right”
<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}"
/>