1. 程式人生 > >wpf 只讓視窗保留一個,並在最前顯示

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