有什么方法可以阻止 WPF 弹出窗口在屏幕外重新定位?

Is there any way to stop a WPF Popup from repositioning itself when it goes off-screen?(有什么方法可以阻止 WPF 弹出窗口在屏幕外重新定位?)
本文介绍了有什么方法可以阻止 WPF 弹出窗口在屏幕外重新定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以阻止 WPF Popup 在离开屏幕时重新定位?

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.

推荐答案

正如 Andrei 所指出的,这种行为在 Popup 控件的深处并且难以克服.如果您愿意做一些工作,可以通过在弹出窗口到达屏幕边缘时调整大小和翻译内容来完成.出于演示的目的,我们将重点放在屏幕的左边缘.

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;
}

然后通过计算 Popup 会在屏幕外,我们可以减少内容的宽度并给它一个负边距,以便屏幕上的部分被剪裁到如果 Popup 允许这样做,就会出现.

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 弹出窗口在屏幕外重新定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)