Window shown as dialogue losts its focus - wpf - c#

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

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.

Material Design DialogHost doesn't close after First Page Load in WPF

I've been working with this WPF application that integrates a UrhoSharp Window. When I click somewhere in the Urho window, there's this dialog that pops up as rendered by the main WPF Application.
The issue is that on every application startup or first page load, the dialog pops as it should but doesn't respond to any of the user actions (the dialog has buttons in it) which is supposed to close the dialog eventually. However, if I resize the window or just click the application in the Start Bar, the dialog now responds(happily ever after). This issue doesn't return until I exit the current application page and switch back to the same page. The case is also the same for every application startup.
All the buttons in the dialog executes this command:
ICommand cmd = DialogHost.CloseDialogCommand;
Execute(null);
And the CloseDialogCommand is awaited like this:
var dialog = new SomeDialog();
dialog = (SomeDialog)await Controller.dialogHandler(dialog, true);
I couldn't figure out what the issue actually is. May be some help would do good.

Opening a windows relative to its tab in WPF

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

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