WPF Dockpanel 不会正确对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15502881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:10:27  来源:igfitidea点击:

WPF Dockpanel won't align right

wpfdockpanel

提问by PitAttack76

I have this code on which I use a DockPanel on a Button.Content. However it doesn't let me right align the last image (small arrow).

我有这个代码,我在 Button.Content 上使用了 DockPanel。但是它不允许我正确对齐最后一个图像(小箭头)。

<Button Height="70"
                    HorizontalContentAlignment="Left">
              <Button.Content>
                <DockPanel LastChildFill="False">
                  <Image DockPanel.Dock="Left"
                    Height="50"
                         Width="50"
                         Source="{StaticResource Placeholder}"
                         Stretch="UniformToFill"
                         Margin="5" />
                  <TextBlock DockPanel.Dock="Left"
                             Text="Dummy text"
                             VerticalAlignment="Center"
                             Margin="5" />
                  <Image DockPanel.Dock="Right"
                         Height="24"
                         Width="24"
                         Source="{StaticResource Right_Arrow_24}"
                         VerticalAlignment="Center"
                         HorizontalAlignment="Right"
                         Stretch="UniformToFill"
                         Margin="5" />
                </DockPanel>
              </Button.Content>
            </Button>

It now gives me this:

它现在给了我这个:

DockPanel

码头面板

So the right small arrow should be placed on the right of the button and not just after the TextBlock. I've found some similar issues and it looks like I did it correct, but somehow it isn't..

所以右边的小箭头应该放在按钮的右边,而不是放在 TextBlock 之后。我发现了一些类似的问题,看起来我做对了,但不知何故它不是..

What am I doing wrong here?

我在这里做错了什么?

采纳答案by mathieu

Try to set the HorizontalContentAlignmentof your button to "Stretch". Otherwise your DockPanel will use the size need by its content, then align left. You can confirm this behaviour by using different text lengths for your TextBlocks

尝试将HorizontalContentAlignment按钮的设置为“ Stretch”。否则您的 DockPanel 将使用其内容所需的大小,然后左对齐。您可以通过为您的TextBlocks使用不同的文本长度来确认此行为

回答by rpaulin56

You simply have to append an extra child to the DockPanel (say an empty Canvas) without the DockPanel.Dock attribute, so that all the remaining space is assigned to it. In general, DockPanel only works fine only when its last child has no Dock constraint

您只需在没有 DockPanel.Dock 属性的情况下将额外的子项附加到 DockPanel(比如一个空的画布),以便将所有剩余空间分配给它。一般来说,DockPanel 只有在它的最后一个子节点没有 Dock 约束时才能正常工作

回答by Brandon Moretz

Try setting

尝试设置

HorizontalContentAlignment="Stretch"

Horizo​​ntalContentAlignment="拉伸"

On your button.

在你的按钮上。