弹出控件VerticalAlignment不会“捕捉"到弹出窗口. [英] Popup Control VerticalAlignment doesn't "catch"

查看:92
本文介绍了弹出控件VerticalAlignment不会“捕捉"到弹出窗口.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Popup控件,由于某种原因,它的垂直对齐方式不符合我的预期.这是我拥有的XAML:

I'm using a Popup control, and for some reason its vertical alignment doesn't work as I expect. This is the XAML I have:

    <Grid Width="100" Height="30" >
        <TextBox Text="Hello" />
        <Popup IsOpen="True" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" >
            <Button Content="Hello" />
        </Popup>
    </Grid>

我希望Popup的Width为100px,与其父网格相同. 但是,弹出窗口中的Button的行为就像其Horizo​​ntalAlignment为Left一样,也就是说,Button的宽度刚好足以在其中允许单词"Hello",而我不知道为什么或如何使它具有与包含网格的宽度相同.

I expected that the Popup's Width would be 100px, the same as it's parent Grid. However, the Button inside the popup behaves as if its HorizontalAlignment is Left, i.e., the Button's width is just enough to allow the word "Hello" inside of it, and I can't figure out why, or how to make it have the same width as the containing Grid.

谢谢!

推荐答案

不幸的是,Popup与定义XAML的其余元素不在同一视觉树中.它实际上是一个新窗口,尽管它是一个非常简单且通常很小的窗口.

Unfortunately, a Popup is not part of the same visual tree as the rest of the elements in the XAML in which it is defined. It is literally a new window, albeit a very simple and typically small window.

换句话说,Popup并没有真正参与主窗口的布局.因此,更改HorizontalAlignment之类的内容无效,因为该属性控制此元素相对于其包含元素的布局位置和大小.

Put another way, the Popup doesn't really participate in the layout of the main window. So changing things like HorizontalAlignment has no effect because that property controls how layout positions and sizes this element relative to its containing element.

在处理Popup时,此孤立元素"问题不会引起任何问题.尽管如此,还是有工具和技术来解决所有问题,这是一个单独的窗口.

This "orphaned element" issue causes no end of problems when dealing with a Popup. Nevertheless, there are tools and techniques to address all the problems that being a separate window introduces.

我可以提出的第一个建议是了解放置属性PlacementTargetPlacement.您可以使用这些属性来定位Popup.您还可以使用数据绑定的ElementName语法来调整大小.

The first suggestion I can give is to understand the placement properties, PlacementTarget and Placement. You can use these properties for positioning the Popup. You can also use the ElementName syntax of databinding to take care of sizing.

以下是在您的情况下使用这些技术的示例:

Here is an example of using these techniques in your situation:

<Grid Name="grid" Width="100" Height="30" >
    <TextBox Text="Hello" />
    <Popup IsOpen="True"
           Placement="Bottom"
           PlacementTarget="{Binding ElementName=grid}"
           Width="{Binding ActualWidth, ElementName=grid}">
        <Button Content="Hello" />
    </Popup>
</Grid>

这篇关于弹出控件VerticalAlignment不会“捕捉"到弹出窗口.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆