iterating through open windows in wpf - wpf

i'm developing an application (wpf) that have 3 windows. in the main window user can open other window. when user wants to open window1 i'm create an instance of window1 and showing up that
var win1 = new Window1();
win1.Owner = this;
win1.Show();
now when user wants to close app, i want to iterate through each open window and check if that window isn't busy (or if is busy wait to complete operation) close that window and then close application. my question is how to iterating through open windows? maybe using this:
foreach (var window in Application.Current.Windows)
{
window.
}
but how i can detect window is Window1 or Window2?

You can do
if(window is Window1)
{
//window is of type 'Window1'!
}
Inside the 'foreach' loop.

Related

How to restrict opening of the same window again and maximized the window if its minimized like Outlook using WPF

I have a WPF project which have list box collections. When i clicked one of the items from the list box collections, the respective item's detailed information's opened with separate window. We can have multiple window like Outlook.
If i click on one item from list box, it will open with new window, if i click the same item means another window open with same information. In outlook if already opened means it will maximized the window which is already opened.
To get all windows opened in you application use this method :
public Window getWindow(String Title)
{
Window windowObject = null;
foreach (Window window in System.Windows.Application.Current.Windows)
{
if (window.Title == Title)
{
windowObject = window;
}
}
return windowObject;
}
Of course, you will have to provide the title, which i think can be found on your ListBoxItem Content.
Use this to maximize it:
windowObject.WindowState = WindowState.Maximized;
Edit 1
It works for me, on the GUI thread i am getting the Windows collection. I have pinned the items in that collection for the sake of accuracy.
As you see on my Loaded event :
for (int i = 0; i < 2; i++)
{
Window wnd = new Window();
wnd.Title = "Title " + i.ToString();
wnd.Show();
}
I am instantiating two new fresh windows and they are shown up in the snip. I am not sure what more to say...

To set the owner property for WPF window

I am Working on creating addin for Autodesk Inventor, I have class lib project in that I will show a wpf window on button click, that works great. However, I could not set the owner property for my window.. In my research I came to know that we need to get the parent window object..
If you're not able to get parent window, you can try setting the parent using window handle.
I'm not familiar with Autodesk Inventor and how you create plugin for the application so I don't know if you can get window handle but I guess you could know process id or window caption/title or some other information that can help you get the parent window handle (you should Google how to get window handle). Once you have handle of parent window you can set it as an owner of your window using WindowInteropHelper.
Here's just a sample how to use WindowInteropHelper:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
IntPtr parentWindowHandler = IntPtr.Zero;
// I'll just look for notepad window so I can demonstrate (remember to run notepad before running this sample code :))
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains("Notepad"))
{
parentWindowHandler = pList.MainWindowHandle;
break;
}
}
var interop = new WindowInteropHelper(this);
interop.EnsureHandle();
// this is it
interop.Owner = parentWindowHandler;
// i'll use this to check if owner is set
// if it's set MainWindow will be shown at the center of notepad window
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
}
}
I'm just reiterating the example that user1018735 gave. I develop Inventor Add-ins so I modified the code above to make sure I am always working with the correct Inventor session since multiple instances of Inventor can be open at once. I do this by passing my already known application object to my form through the _App parameter; then since the process MainWindowTitle is always the same as the applications caption I match the two.
I'm running this in a WPF Class Library # VB.Net 4.5.1.
Here is a peek at my code that works in Inventor 2014...
Public Sub New(ByVal _App As Inventor.Application)
'This call is required by the designer.
InitializeComponent()
'Find the Inventor Window Handle.
Dim InvWndHnd As IntPtr = IntPtr.Zero
'Search the process list for the matching Inventor application.
For Each pList As Process In Process.GetProcesses()
If pList.MainWindowTitle.Contains(_App.Caption) Then
InvWndHnd = pList.MainWindowHandle
Exit For
End If
Next
Dim InvWndIrp = New WindowInteropHelper(Me)
InvWndIrp.EnsureHandle()
InvWndIrp.Owner = InvWndHnd
...
// Create a window and make this window its owner
Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.Show();

Modeless Child WPF Window to a native MFC MDI Application

I have an MFC MDI application and I am trying to add a new dialog to it. I want this dialog to be in WPF (a Window basically rather than a dialog). This window should be modeless and a child to the current MDI View.
Let's say I have CMyView in the MFC application, and in its OnCreate, I try to create the WPF Window. To do so, I made a wrapper class called CMyWindowWrapper (that compiles with /CLR)
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_wrapper.Create(this);
return 0;
}
The window wrapper class has a Create function which actually creates the WPF Window:
void CMyWindowWrapper::Create(CWnd* pParent)
{
MyWindow^ window = gcnew MyWindow();
window->ShowModeless((IntPtr)pParent->GetSafeHwnd());
m_myWindow = window;
}
MyWindow is the WPF Window where I added a function called ShowModeless as follows:
public void ShowModeless(IntPtr parent)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = parent;
Show();
ShowInTaskbar = false;
}
Now the application behaves as follows: whenever a CMyView is created, a modeless MyWindow is created successfully, and it appears always on top of CMyView even if the focus is on CMyView. However, when CMyView is closed or minimized, MyWindow is not following it. It gets close/minimized only if the whole application gets closed/minimized.
I can attach a sample application showing the problem if needed.
Please advise.
Thank you so much.
An alternative solution would be to make your WPF window a user control. Create a MFC modeless dialog and put the WPF user control in the MFC modeless dialog.

Open a window after the mainWindow and activate it

In my application is required the user to select an item from a list before continues uses the application.
To do this, I have a window with the desired items and I display it when the MainWindow displays.
public MainWindow()
{
InitializeComponent();
var itemsWindow = new ItemsWindow();
itemsWindow.Show();
}
The problem is that the window opens in background. How can I open the window in foreground?
The preferable would be to open the itemsWindow on applications start up and onClose event of itemsWindow to display the mainWindow, but I think this approach is far away from my knowledge. Nevertheless, I would appreciate it if someone could post something for how to achieve this.
Thanks
Use ShowDialog() method instead. That way, you won't have to worry about activating that window and user won't be able to interact with MainWindow until ItemsWindow has been closed.
Example:
var itemsWindow = new ItemsWindow();
itemsWindow.ShowDialog();

Make child window always on top of all windows

I'm writing in wpf. In my viewModel I have a command that opens new window. However sometimes this child window is placed under the parent window. (if for instance I work in my application, then open browser and want to return to my application). Window is opened as follows:
MyViewModel vm = new MyViewModel(oper);
Mywindow window = new MyWindow();
//Initialize viewModel and set window.DataContext to this viewModel
CWPFWindowWithViewModelHelper<IWindowWithViewModel>.InitializeViewModel(window, vm);
window.ShowDialog();
I want this child window to be always visible when opened. How can I do this?
just try with
window.Owner=this
window.TopMost = true;

Resources