Opening a windows relative to its tab in WPF - c#

I've a WPF application that makes use of AvalonDock for opening multiple tabs ...each tab represents a function and has it's own controls .. The single tab is opened via menu.
Sometimes I need to show a window in the current tab and I use the
MyWindow w = new MyWindow();
w.Show(); //w.ShowDialog();
this opens a popup(/modal) but when I switch the tab (in the case it's not modal) I have the Window still open... what I need is to "force" the window to be shown only in it's tab.
I've tried setting the Owner of the window but when I do
var wnd = Window.GetWindow(docuentPane);
I got the main window of the app...
is it possible to do what I want to achieve in WPF? if yes how?
Thanks in advance

Related

Why does a folder dialog immediately close unless a window is displayed in WPF?

I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.

WPF Window under all other windows after making window visible from being collapsed

I am having some very frustrating issues with my WPF window. The design I was going for is:
When the user minimizes the window it will minimize to a system tray icon (hide the window and icon on start bar). When the user right clicks on the icon, a context menu will show up with options and one of them will say open, which will open the app (show window again / unhide window agian). The user could also double click the icon as well.
Pretty simple right?
I have no issues minimzing to the task bar, I simply do the following on the closing event of the window:
e.Cancel = true;
this.Visibility = Visibility.Collapsed;
However, I am having issues properly restoring the window. I simply do this on the context menu click or icon double click event:
this.Visibility = Visibility.Visible;
this.Activate();
The issue is that the window is once again on the start bar with its icon but it is behind every single window the user has open. I want it so when the user goes to open the window it will be the top most window. I do not always want it to be the topmost, just only when they want to make it visible again.
I have tried many things like setting the show activate on the window to true, waiting a second after making it visible to then activate the window, activating the window multiple times (worked a few times but was maybe 1 out of 10 tries), etc..
I don't think showing / hiding a window should be this annoying and I am not really sure what I am doing wrong.
Any help is appreciated, thank you.
Only after posting this did I realize, the application minimizes first before hiding. When I show the window, it was showing as minimized.
After knowing this issue I was able to fix the issue. This may help others who do decide to hide the window after minimizing.
EDIT
Here is the code I used to hide the window (this is called after the event fires for state changed [minimized]):
Application.Current.MainWindow.Visibility = Visibility.Collapsed;
Application.Current.MainWindow.WindowState = WindowState.Normal;
You will notice I set the window state back to normal after I hide it. Even though the window is hidden and not being rendered it will in memory restore the window location / size.
Then when I want to see the window again I just do:
Application.Current.MainWindow.Visibility = Visibility.Visible;
Which will show the window just fine!
Hopes this helps someone out there!

Window shown as dialogue losts its focus - wpf

I have a main window Say home and from there i am calling another window named addItem as
var item = new addItem();
item.ShowDialog();
Its working fine.
But when we navigate to other applications like chrome, notepad by alt + tab and come back to the WPF application both the windows are separated meaning as like in winforms it wont stick together
So user got confused in that behavior.
Requirement is unless until the dialogue window is opened it should always
be on top and main window on the back and when we click the icon in task bar both window together should come.
try this
var item = new addItem();
item.Owner = this;
item.ShowDialog();

How to open a child windows under parent window on menu item click in WPF?

I am developing an application in C#. I'm using .Net under WPF. I want to open a new child window (like a dialog) and it should open within the main window, just below the menu bar.
However, the newly opened windows is simply showing an OS task bar. How can I open the various new windows(like a dialog box) under the main window?
Try this.
MyChildWindow cw = new MyChildWindow();
cw.ShowInTaskbar = false;
cw.Owner = Application.Current.MainWindow;
cw.Show();

How do you send information from one window to another in the same program?

In the program I am trying to build, I have a menu button that opens a second window. The user puts information into the second window, presses the "Done" button, and the information is transfered into the main window. The problem I am having is opening the second window. I have both windows build in xaml files in Visual Studio but I can't find a way to show the second window. Using "Window window = new Window" does not fit my needs because 1) I already have the second window built and 2) I have tried this and I cannot figure out how to add children to the window; there is no window.children nor any grid to put the children into. Thank you in advance!
Moments after I pressed post, I thought of something I hadnt tried:
"WindowAdd add = new WindowAdd; //WindowAdd being the second window
add.Show();"
This does exactly what I want it to do. The next problem I have is sending the information the TextBoxes into the MainWindow. I am thinking cookies might work but am unsure. Anyone have any thoughts? Thanks in advance!
You need to create the Window in code, but instead of doing:
Window window = new Window();
You should use:
Window2 window = new Window2(); // Assuming the window's class name is Window2
This will construct and initialize an instance of your new window class, defined in XAML. Once you've done this, you can open the window and you'll see all of your controls.

Categories

Resources