wpf 只让窗口保留一个,并在最前显示

就是遍历所有的,有的话就不生成了,没有的话就生成一个,还有最前是一个参数,最后就是窗口事件了。

ChangeDistingWordOfSelected mw = null;
            foreach (Window win in App.Current.Windows)
            {
                if (win is ChangeDistingWordOfSelected)
                {
                    mw = win as ChangeDistingWordOfSelected;
                }
            }
            if (mw == null)
            {
                mw = new ChangeDistingWordOfSelected();
                mw.WindowStartupLocation = WindowStartupLocation.Manual;
               // mw.Left = 1025;//窗口生成的位置
                mw.Left = Mouse.GetPosition(GlobalHelper._mainWindow).X;
                mw.Top = Mouse.GetPosition(this).Y ;
                mw.Show();
            }
            else
            {
                mw.Visibility = System.Windows.Visibility.Visible;
                mw.Topmost = true;
            }

            mw.btn_ok.Click += delegate//确定替换字
            {
                ListResult[CurrentLineIndex].LineTxtInfo[CurrentTxtIndex].TxtResult = mw.ChangedWord.Text;//更换选中的字;

                ObservableCollection<string> strNewLineTxt = new ObservableCollection<string>();
                strNewLineTxt.Clear();
                for (int l = 0; l < ListResult[CurrentLineIndex].LineTxtInfo.Count; l++)
                {
                    strNewLineTxt.Add(ListResult[CurrentLineIndex].LineTxtInfo[l].TxtResult);
                }
                this.ListListBox[CurrentLineIndex].ItemsSource = strNewLineTxt;
                mw.Close();
            };

2,也可以新那家一个全局的窗口对象,每次检查当前对象是否为空

if (drawingsoftware_wnd == null)
            {
                double height = this.Height;
                double width = this.Width;
                drawingsoftware_wnd = new DrawingSoftware_Popup(this, height, width);
                drawingsoftware_wnd.Owner = this;
            }

            if (!drawingsoftware_wnd.IsVisible)
            {
                drawingsoftware_wnd.Show();
                this.Hide();
            }
            else
            {
                drawingsoftware_wnd.Hide();
            }

posted on 2017-09-29 13:41  蝸小牛  阅读(478)  评论(0编辑  收藏  举报

导航