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

wpf 只讓窗口保留一個,並在最前顯示

cli sof += proc mouse glob wid source ner

就是遍歷所有的,有的話就不生成了,沒有的話就生成一個,還有最前是一個參數,最後就是窗口事件了。

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

wpf 只讓窗口保留一個,並在最前顯示