防止弹出窗口停留在新启动的模态窗口之上

时间:2013-01-31 13:06:47

标签: c# .net wpf mvvm

我有一个使用弹出窗口的窗口。弹出窗口按规范要求保持打开状态。该窗口还会启动一个新的模态窗口。我遇到的问题是新的模态窗口出现在原始窗口的顶部而不是弹出窗口。弹出窗口显示在所有内容之上,使得它看起来非常奇怪。

3 个答案:

答案 0 :(得分:1)

我对Adrian Faciu提到的NonTopmostPopup表示祝你好运。它只是它的父窗口的最顶层,所以,假设你的模态窗口确实是一个新的Window(不仅仅是另一个看起来像一个的控件),它应该运行良好。如果我记得,它唯一的一个小问题是非常偶然的闪烁或刷新周期,这取决于在它前面拖动的Windows等等(但对我来说,不使用它是不够的问题)。

答案 1 :(得分:0)

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

据我所知,据我所知,Popup始终显示在顶部,我无论如何都没有找到避免这种情况。

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

答案 2 :(得分: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;
        }
    }
}