有没有办法从自身重新定位停止WPF弹出,当它熄灭屏幕? [英] Is there any way to stop a WPF Popup from repositioning itself when it goes off-screen?

查看:200
本文介绍了有没有办法从自身重新定位停止WPF弹出,当它熄灭屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法阻止一个WPF 弹出从当它熄灭屏幕重新定位自己?

Is there any way to stop a WPF Popup from repositioning itself when it goes off-screen?

我发现这个的老问题,但它没有得到正确的答案。有没有办法做到这一点?我愿意在必要时继承它。谢谢你。

I found this old question, but it didn't get a proper answer to it. Is there any way to do this? I'm willing to subclass it if necessary. Thanks.

推荐答案

由于安德烈指出,这种行为是深弹出窗口里面控制和难以克服。如果你愿意做一些工作是可以通过调整和翻译,当它到达屏幕边缘弹出窗口的内容来完成。对于演示的目的,我们将专注于屏幕左侧边缘

As Andrei points out, this behavior is deep inside the Popup control and hard to overcome. If you are willing to do some work it can be done by resizing and translating the content of the popup when it reaches the screen edges. For the purposes of the demonstration, we'll focus on the left edge of the screen.

如果我们有一些XAML是这样的:

If we have some XAML like this:

<Window ...
        LocationChanged="Window_LocationChanged"
        SizeChanged="Window_SizeChanged"
        >
    <Grid>
        <Rectangle Name="rectangle1" Width="100" Height="100" Fill="Blue"/>
        <Popup Name="popup1" PlacementTarget="{Binding ElementName=rectangle1}" IsOpen="True" Width="100" Height="100">
            <TextBlock Background="White" TextWrapping="Wrap" Width="100" Height="100">
                <TextBlock.Text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</TextBlock.Text>
            </TextBlock>
        </Popup>
    </Grid>
</Window>

和后台代码是这样的:

private void Window_LocationChanged(object sender, EventArgs e)
{
    RefreshPopupPosition();
}

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    RefreshPopupPosition();
}

private void RefreshPopupPosition()
{
    var upperLeft = rectangle1.PointToScreen(new Point(0, 100));
    var xOffset = Math.Min(0, upperLeft.X);
    popup1.Width = xOffset + 100;
    (popup1.Child as FrameworkElement).Margin = new Thickness(xOffset, 0, 0, 0);
    popup1.HorizontalOffset += 1;
    popup1.HorizontalOffset -= 1;
}



通过计算则认为弹出将关闭屏幕,就可以减少内容的宽度并给它一个消极的利润率,这样即屏幕上的部分剪切到什么会出现,如果弹出分别以允许此

then by calculating that the Popup would be off-screen, we can reduce the width of the content and give it a negative margin so that the portion that is on-screen is clipped to what would have appeared if the Popup were to allow this.

这将必须被扩展以处理画面和多个画面的可能性的所有四个边,但它表明该方法是可行的。

This would have to be extended to deal with all four edges of the screen and the possibility of multiple screens, but it demonstrates that the approach is workable.

这篇关于有没有办法从自身重新定位停止WPF弹出,当它熄灭屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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