将窗口保持在顶部WPF [英] Keep window on top WPF

查看:102
本文介绍了将窗口保持在顶部WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Window保持在TOP上有问题吗?我使用MVVM WPF

I have problem to keep Window on TOP ? I work with MVVM WPF

我在ConfigRole模型中有以下代码:

I have this code in ConfigRole Model:

if (!System.Windows.Application.Current.Windows.OfType<ConfigRole>().Any())
        {                 
            ConfigRoleModel configRoleModel = new ConfigRoleModel();
            ConfigRole winconfigRole = new ConfigRole();
            winconfigRole.DataContext = configRoleModel;
            winconfigRole.Show();
            winconfigRole.Topmost = true;
            winconfigRole.Focus();
        }

这里的新窗口是正确的,它在TOP上,

Here the new Window is Correct, it is on TOP,

但是在那之后,我想在ConfigRoleModel的TOP上显示其他窗口,这是代码ConfigRoleModel:

but after that, I want to show other Window on TOP from ConfigRoleModel , this is code ConfigRoleModel :

if (!System.Windows.Application.Current.Windows.OfType<ButtonListView>().Any())
         {                 
    ButtonListViewModel buttonListViewModel = new ButtonListViewModel();
             ButtonListView winconfigRole = new ButtonListView();
             winconfigRole.DataContext = buttonListViewModel;

             winconfigRole.Show();
             winconfigRole.Topmost = true;
             winconfigRole.Focus();
         }

所以,这里我没有这个新的TOP窗口!我不明白!它与第一个..

So ,here I don't have this new Window on TOP! I don't understand! it is the same code like the first ..

我也尝试

winconfigRole.ShowDialog();

Window.activate()


还有同样的问题!

我该如何解决?

谢谢

推荐答案

var owner = System.Windows.Application.Current.Windows.OfType<Window>().FirstOrDefault(w=>w.IsActive);
var vm = new ButtonListViewModel();
var dialog = new ButtonListView()
      {
          DataContext = vm,
          Owner = owner,
      };

if ( dialog.ShowDialog() == true)
{
   //TODO: ...
}



这篇关于将窗口保持在顶部WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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