弹出控件随父控件移动 [英] Popup control moves with parent

查看:27
本文介绍了弹出控件随父控件移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这对您的 WPF 专家来说是一个快速的选择.是否有任何简单的 XAML 解决方案可以让我的弹出窗口在寡妇移动或更改大小时移动?代码如下.如果没有,我总是可以在后面的代码中处理一个事件.

Hopefully this is a quick one for you WPF experts out here. Is there any easy XAML solution to allow my popup to move when the widow moves or changes size? The code is below. If not, I can always handle an event in the code behind.

<Grid>
    <Canvas>
        <Expander Header="details" HorizontalAlignment="Center" VerticalAlignment="Top" ExpandDirection="Down"
                  Expanded="Expander_Expanded" Panel.ZIndex="99" Collapsed="Expander_Collapsed" Name="expander">

                <Popup PopupAnimation="Slide" Name="popup" Width="200" Height="200" StaysOpen="True" AllowsTransparency="True" 
                       IsOpen="False" >
                <Grid Background="Cornsilk">
                    <Grid.BitmapEffect>
                        <DropShadowBitmapEffect/>
                    </Grid.BitmapEffect>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                   <TextBlock TextWrapping="Wrap" FontWeight="Bold">
                      Some example text
                   </TextBlock>
                </Grid>
            </Popup>

        </Expander>
    </Canvas>
</Grid>

推荐答案

这里有一个解决方案:

这个纯标记示例由一个文本框和一个弹出窗口组成.当文本框聚焦时,弹出窗口会打开.弹出窗口通过挂钩窗口的位置更改事件并强制弹出窗口相应地重新定位自身来跟随窗口移动.

This markup-only sample consists of a text box and a popup. The popup opens when the text box is focused. The popup follows the window as it moves by hooking the window's location changed event and forcing the popup to reposition itself accordingly.

<Grid>
    <StackPanel>
        <TextBox x:Name="textBox1" Width="200" Height="20"/>
        <Popup PlacementTarget="{Binding ElementName=textBox1}" IsOpen="{Binding IsKeyboardFocused, ElementName=textBox1, Mode=OneWay}">
            <TextBlock Background="White" Text="Sample Popup content."/>
            <p:Attached.Operations>
                <p:EventHandler Path="Loaded">
                    <p:ScriptHandler Path="@FindAncestor([Window], @AssociatedObject.PlacementTarget).LocationChanged">
                        @AssociatedObject.HorizontalOffset += 1;
                        @AssociatedObject.HorizontalOffset -= 1;
                    </p:ScriptHandler>
                </p:EventHandler>
            </p:Attached.Operations>
        </Popup>
    </StackPanel>
</Grid>

这篇关于弹出控件随父控件移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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