0

我有一个使用弹出窗口的窗口。弹出窗口设置为按照规范的要求保持打开状态。此窗口还会启动一个新的模式窗口。我遇到的问题是新的模式窗口出现在原始窗口的顶部,而不是弹出窗口。弹出窗口出现在所有内容之上,这一切看起来都很奇怪。

4

3 回答 3

1

Adrian Faciu 提到的NonTopmostPopup让我很幸运。它仅位于其父窗口的最顶层,因此,假设您的模态窗口确实是一个新的Window(不仅仅是另一个看起来像一个的控件),它应该可以正常工作。如果我记得,它唯一的小问题是一个非常偶然的闪烁或刷新期,具体取决于被拖到它前面的 Windows 等(但对于我来说,不使用它还不足以成为问题)。

于 2013-01-31T16:07:27.227 回答
0
public class PopupParentWindowFocusBehavior : Behavior<Popup>
{
    private bool _hidden;
    private UIElement _lastPlacementTarget;
    private System.Windows.Window _lastWindow;
    private PropertyChangeNotifier _placementTargetNotifier;

    protected override void OnAttached()
    {
        base.OnAttached();
        InitializeForPlacementTarget();

        _placementTargetNotifier = new PropertyChangeNotifier(AssociatedObject, Popup.PlacementTargetProperty).AddValueChanged(OnPlacementTargetChanged);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        DetachWindowEvents();

        if (_lastPlacementTarget != null)
        {
            ((Control)_lastPlacementTarget).Loaded -= OnPlacementTargetLoaded;
        }
        _placementTargetNotifier.ValueChanged -= OnPlacementTargetChanged;
    }

    private void OnPlacementTargetChanged(object sender, EventArgs e)
    {
        InitializeForPlacementTarget();
    }

    private void InitializeForPlacementTarget()
    {
        if (_lastPlacementTarget != null)
        {
            ((Control)_lastPlacementTarget).Loaded -= OnPlacementTargetLoaded;
        }
        if (AssociatedObject.PlacementTarget != null)
        {
            ((Control)AssociatedObject.PlacementTarget).Loaded += OnPlacementTargetLoaded;
            AttachWindowEvents();
        }
        _lastPlacementTarget = AssociatedObject.PlacementTarget;
    }

    private void OnPlacementTargetLoaded(object sender, RoutedEventArgs e)
    {
        AttachWindowEvents();
    }

    private void OnWindowClosed(object sender, EventArgs e)
    {
        DetachWindowEvents();
    }

    private void AttachWindowEvents()
    {
        if (_lastWindow != null)
        {
            DetachWindowEvents();
        }

        System.Windows.Window window = System.Windows.Window.GetWindow(AssociatedObject.PlacementTarget);
        if (window != null)
        {
            window.Deactivated += OnWindowDeativated;
            window.Activated += OnWindowActivated;
            window.Closed += OnWindowClosed;
        }

        _lastWindow = window;
    }

    private void DetachWindowEvents()
    {
        if (_lastWindow != null)
        {
            _lastWindow.Deactivated -= OnWindowDeativated;
            _lastWindow.Activated -= OnWindowActivated;
            _lastWindow.Closed -= OnWindowClosed;
        }
    }

    private void OnWindowDeativated(object sender, EventArgs e)
    {
        System.Windows.Window window = System.Windows.Window.GetWindow(AssociatedObject.PlacementTarget);
        if (window != null && AssociatedObject.IsOpen)
        {
            _hidden = true;
            AssociatedObject.IsOpen = false;
        }
    }

    private void OnWindowActivated(object sender, EventArgs e)
    {
        if (_hidden)
        {
            _hidden = false;
            AssociatedObject.IsOpen = true;
        }
    }
}
于 2013-03-06T16:20:34.213 回答
0

Popup 是一个控件,它在 XBAP 中非常有用,在 XBAP 中您不能(或者如果您这样做,您会收到安全警告)由于权限而打开第二个窗口,并且 Popup 是显示消息框的一种方便的方式。如果您在普通 WPF 中工作,我建议您使用普通窗口(如果需要,使用 WindowStyle.None)而不是弹出窗口。

说,据我所知,弹出窗口总是显示在顶部,我无论如何都没有找到避免这种情况的方法。

参考 :: http://social.msdn.microsoft.com/Forums/en/wpf/thread/7467957a-08e9-4e9b-a03f-51c79a94bccb

于 2013-01-31T13:11:11.630 回答