In regard to the code below.
If I use the built in MessageBox, then the previous MessageBox has to be closed before the next one is displayed.
How can I achieve this with a Window so that I can create a custom message box? I tried using the ShowDialog method, but whilst this does create Modal windows, it still shows them all at the same time cascaded.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 3; ++i)
{
Dispatcher.BeginInvoke(new Action(() => ShowDialog2()));
}
}
void ShowDialog2()
{
//MessageBox.Show("A message");
Window w = new Window() { Width = 200, Height = 200, Content = "SomeText" };
w.ShowDialog();
}
}
Open first instance of window with ShowDialog and consequent instances of window with Show method.
Show open a non-modal window whereas ShowDialog open modal window.
Related
I have a ViewModel defined like
public class PlayerViewModel : Screen, IDiscoverableViewModel
I am showing a dialog pop up as
var result = await _dialogManager.ShowDialogAsync(item, new List<DialogResult>() { DialogResult.Cancel });
Here item is the another ViewModel which shows UI from the related View. This pop up is showing some information and needs to be auto closed after few seconds in case user doesn't select Cancel button.
Following is the Timer tick event that is getting fired after 10 seconds.
void timer_Tick(object sender, EventArgs e)
{
this.DialogHost().TryClose(DialogResult.Cancel);
}
But it's not working and throwing exception as this.DialoHost() is getting null always. I tried this solution but it is closing the whole ViewModel instead I want to close only the dialog window.
Could you confirm if your 'pop-up viewmodel' is deriving from Screen ? If so, TryClose should work. Could you please verify it ? Sample code for closing.
public class CreatePersonViewModel:Screen
{
System.Timers.Timer _timer = new Timer(5000);
public CreatePersonViewModel()
{
_timer.Elapsed += (sender, args) =>
{
_timer.Enabled = false;
TryClose(true);
};
_timer.Start();
}
}
I have a wpf window that shows Bing map whith som bushpin on it,
The window is getting opened by button click in code behind.
Each time i click on the button, new window shows up with current new data.
How can i close the "allready" opened window and open the a new one with the new data, or maybe refresh the current opened window with the new data.
here is my code:
IList<object> rowsToExport = getRows();
BingMapWindow window = new BingMapWindow(rowsToExport);
// somthing like this
if(window.IsOpened)
window.Close;
window.show();
OR
if(window.IsOpened)
window.refresh();
Have you considered storing reference to that window outside method and just checking it for null?
BingMapWindow window;
private void CloseWindow()
{
if(window != null)
{
window.Close();
window = null;
}
}
private void OpenWindow(BingMapWindow window)
{
this.window = window;
this.window.Show();
}
private void ButtonHandler()
{
CloseWindow();
var bingWindow = new BingMapWindow();
OpenWindow(bingWindow);
}
The following code demonstrates an issue I'm having where closing a child window minimizes the parent window, which I dont want to happen.
class SomeDialog : Window
{
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
new CustomMessageBox().ShowDialog();
}
}
class CustomMessageBox : Window
{
public CustomMessageBox()
{
Owner = Application.Current.MainWindow;
}
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
new SomeDialog() { Owner = this }.Show();
}
}
Window1 is the main application window.
SomeDialog is a window that pops up on some event within Window1(double clicking window1 in the example) that needs to be modeless.
CustomMessageBox is a window that pops up on some event within "SomeDialog" (double clicking SomeDialog in the example) that needs to be modal.
If you run the application, and then double click Window1's content to bring up SomeDialog, and then you then double click SomeDialog's content to bring up the CustomMessagebox.
Now you close CustomMessagebox. Fine.
Now if you close SomeDialog, Window1 minimizes? Why is it minimizing and how can I stop it?
Edit : It appears the workaround is rather simple, using the technique suggesrted by Viv.
class SomeDialog : Window
{
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
new CustomMessageBox().ShowDialog();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
Owner = null;
}
}
Why is it minimizing and how can I stop it?
Not sure about the "Why" maybe you can report it as a bug and see what they reply with as with a non-modal dialog you do not expect this to happen.
As for a workaround, Try something like this:
public partial class MainWindow : Window {
...
protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {
base.OnMouseDoubleClick(e);
var x = new SomeDialog { Owner = this };
x.Closing += (sender, args) => {
var window = sender as Window;
if (window != null)
window.Owner = null;
};
x.Show();
}
}
^^ This should prevent the MainWindow(parent) from minimizing when SomeDialog is closed.
My workaround for this interesting problem is to activate the MainWindow once and after that activate the SomeDialog window again.
class SomeDialog : Window
{
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
new CustomMessageBox().ShowDialog();
Owner.Activate();
Activate();
}
}
A very late answer... I just sat having this same issue and Viv's workaround solved it for me aswell. As for the "Why" part of the answer, i believe it happens when your child window spawns a child window of its own in its lifetime.
For my experience it occured whenever pressing my Save button, which is in a flow which requires a child window to be opened. But pressing the Cancel (or escape) or the windows default quit button did not invoke the issue.
First, as your code stands, I can confirm this strange behaviour. There are two things that I noticed here. The first is that the SomeDialog.Owner is not set, or that it ends up with a null value with this code:
new SomeDialog() { Owner = this }.Show();
Adding this code fixes that problem:
public SomeDialog()
{
Owner = Application.Current.MainWindow;
}
Unfortunately, this doesn't stop the MainWindow from being minimised when the child Window is closed. Then I found that I could stop it from being minimised, but only when calling new SomeDialog().ShowDialog(); instead of new SomeDialog().Show(); However, that makes this Window into a dialog, which is not what you're after I believe.
We had similar problem, but cause was quite simple. Method Close() of Window was called twice. After we had removed second call, all got back to normal.
Im'm using winform DevExpress library.
Now need to create a control, basing on PopupContainerEdit but this control must have some behaviors like when it's focused, the popup opens and when lost focus the popup closes.
This is the code I'm using but the popup dessapears after getting focus.
public class HelpEdit : PopupContainerEdit {
private PopupContainerControl _container;
private GridControl _gridControl;
private GridView _gridView;
[DefaultValue("")]
[DXCategory("Data")]
[AttributeProvider(typeof(IListSource))]
public object Datasource {
get { return _gridControl.DataSource; }
set { _gridControl.DataSource = value; }
}
public HelpEdit() : base() {
_container = new PopupContainerControl();
this.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
this._gridControl = new GridControl();
this._gridControl.Dock = DockStyle.Fill;
this._gridView = new GridView(_gridControl);
_container.Controls.Add(_gridControl);
_container.Size = new Size(this.Width, 250);
this.Properties.PopupControl = _container;
this.Properties.PopupControl.Size = new Size(this.Width, 250);
}
protected override void OnGotFocus(EventArgs e) {
base.OnGotFocus(e);
this.ShowPopup();
}
protected override void OnLostFocus(EventArgs e) {
base.OnLostFocus(e);
this.ClosePopup();
}
}
Your popup disappears because it closes by your code as soon as the popup container control(_container) got focus itself. You should not close popup within the OnLostFocus() override because the base.OnLostFocus method of PopupContainerEdit is already contains correct code for closing popup. Or close popup conditionally, using the following code:
protected override void OnLostFocus(EventArgs e) {
if(IsPopupOpen && !EditorContainsFocus)
ClosePopup(PopupCloseMode.Immediate);
base.OnLostFocus(e);
}
I have a WPF application in which on a click of a menu item a window is opened. If the same menu item is clicked again when the window is already open, it is opening a new window but I don't want a new window to be opened every time.
What I need is, if the window is already open, the same window should be focused not a new window.
//First we must create a object of type the new window we want the open.
NewWindowClass newWindow;
private void OpenNewWindow() {
//Check if the window wasn't created yet
if (newWindow == null)
{
//Instantiate the object and call the Open() method
newWindow= new NewWindowClass();
newWindow.Show();
//Add a event handler to set null our window object when it will be closed
newWindow.Closed += new EventHandler(newWindow_Closed);
}
//If the window was created and your window isn't active
//we call the method Activate to call the specific window to front
else if (newWindow != null && !newWindow.IsActive)
{
newWindow.Activate();
}
}
void newWindow_Closed(object sender, EventArgs e)
{
newWindow = null;
}
I think this solve your problem.
Att,
If your opened windows is used as simple dialog box you can use following code
window.ShowDialog();
when the dialog will show you cannot press any menu items unit you close this window
A rather brute force approach like this also works:
bool winTest = false;
foreach (Window w in Application.Current.Windows)
{
if (w is testWindow)
{
winTest = true;
w.Activate();
}
}
if (!winTest)
{
testWindow tw = new testWindow();
tw.Show();
}
You can create a field and check if it's set:
private Window _dialogue = null;
private void MaekWindowButton_Click(object sender, RoutedEventArgs e)
{
if (_dialogue == null)
{
Dialogue diag = new Dialogue();
_dialogue = diag;
diag.Closed += (s,_) => _dialogue = null; //Resets the field on close.
diag.Show();
}
else
{
_dialogue.Activate(); //Focuses window if it exists.
}
}