简体   繁体   English

当我指定Horizo​​ntalAlignment =“ Right”时,WPF UserControls向左显示

[英]WPF UserControls display left when I specify HorizontalAlignment=“Right”

This is my WPF UserControl code reduced to the relevant part: 这是我的WPF UserControl代码简化为相关部分:

<UserControl x:Class="AKPS.View.UserCalibWindow"
             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"
           >


        <StackPanel  Orientation="Horizontal"  HorizontalAlignment="Right"  Width="1600" >
        <Button   Height="45" Width="100" />
        <Button   Height="45" Width="100" />                
    </StackPanel>

Why do the 2 buttons display on the left? 为什么两个按钮显示在左侧?

You are having a Width for your StackPanel removing that will align your stack panel to right with your controls in it. 您要删除StackPanelWidth将使堆栈面板与其中的控件向右对齐。

<StackPanel  Orientation="Horizontal"  HorizontalAlignment="Right"  >
    <Button Height="45" Width="100" />
    <Button Height="45" Width="100" />
</StackPanel>

A StackPanel does exactly that - it stacks elements together, in your case, horizontally. StackPanel正是这样做的-在您的情况下,它将元素水平堆叠在一起。 Your HorizontalAlignment is referring to the stack panel, which will shift it to the right, not the buttons inside them. 您的HorizontalAlignment指的是堆栈面板,它将向右移动而不是其中的按钮。

Instead, perhaps try using a Grid instead of a StackPanel , and then placing your StackPanel (with no Width element set) inside it. 相反,也许尝试使用Grid而不是StackPanel ,然后将StackPanel (未设置Width元素)放入其中。

HorizontalAlignment controls the container's alignment. HorizontalAlignment控制容器的对齐方式。

Try HorizontalContentAlignment instead. 请尝试使用HorizontalContentAlignment


As Abin indicated, in my haste I'd forgotten StackPanel doesn't have a HorizontalContentAlignment property. 正如阿宾指出的那样,我匆忙地忘记了StackPanel不具有Horizo​​ntalContentAlignment属性。

You could achieve similar functionality with a DockPanel though, or use Abin's solution. 不过,您可以使用DockPanel实现类似的功能,也可以使用Abin的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM