Event trigger (button) opening multiple instances of same window - c#

In my WPF application, I have a main window (Branch.xaml) which has a button that will open an other window (Location.xaml). Once this Location window is open, how do I prevent another instance of this Location window from opening, when the user clicks the same button again?
Or how can I re-focus the same open window, when the user clicks the button again?
The button click code is auto-generated code when you double-click on a button in xaml.
In the "Branch.xaml.cs" file, the code for button click is as follows:
private void rbtn_Location_Click(object sender, RoutedEventArgs e)
{
Location location = new Location();
location.Show();
}
Location is a custom class which opens a window with 3 list boxes
Thanks, any help is appreciated.
I'm using WPF application on C# 4.0 & Visual Studio 2010.

You could create a field in your main window which holds a reference to the location window if there is any, in the button click handler check if the field is null and if it is create a new window and store it in the field, if not call Activate on the window in the field. You also would have to subscribe to the Closed event of the location window to clear out the reference again when the location window is gone.
Edit: Concrete example:
private LocationWindow locationWindow;
private void Button1_Click(object sender, RoutedEventArgs e)
{
if (locationWindow == null)
{
locationWindow = new LocationWindow();
locationWindow.Closed += (s, _) => locationWindow = null;
locationWindow.Show();
}
else
{
locationWindow.Activate();
}
}

Application.Current.Windows collection holds reference for all windows for current AppDomain. You can check for your window in that collection and if it founds your window then call Activate for that window else create new Window. This will get you going -
private void rbtn_Location_Click(object sender, RoutedEventArgs e)
{
Window window = Application.Current.Windows.OfType<Window>().Where(win => win.Name == "LocationWindow").FirstOrDefault();
if(window == null)
{
Location location = new Location();
location.Show();
}
else
{
window.Activate();
}
}
Make sure you provide your window a x:Name as LocationWindow to make it work.
<Window x:Name="LocationWindow">
</Window>
Also include namespace System.Linq in your code behind.

Related

Pop-up window in a WPF application

I've a C# WPF application which show uses Grid control in the xaml(P screen).For every row in the grid, I've a column called Details.Clicking on item in this column shows a pop-up windows which also has a grid in the xaml(C screen).
My item click event in the P's viewmodel has the following code:
var myChildWindow = new MyGridView();
myChildWindow.Show();
If the user clicks on the item multiple times, I just want to highlight the existing C pop-up window.If there's no existing windows open, then only I want to open a new windows.
I've worked on a similar requirement for Winforms applicaiton.How do I go about this for a WPF application please?
Thanks.
First you'd need to declare myChildWindow outside of the click event so that it is accessible from multiple events. So,
MyGridView myChildWindow;
goes outside the click event, probably as a private variable.
Then, in your click event see if it's null, and if it is, create it.
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Show();
}
You could keep a reference to the window and get rid of this when the window is closed:
MyGridView myChildWindow;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (myChildWindow == null)
{
myChildWindow = new MyGridView();
myChildWindow.Closed += MyChildWindow_Closed;
myChildWindow.Show();
}
else
{
myChildWindow.Activate();
}
}
private void MyChildWindow_Closed(object sender, EventArgs e)
{
myChildWindow.Closed -= MyChildWindow_Closed;
myChildWindow = null;
}

How to launch method when window got focus again in WPF?

My app is multi-window, here is quickly how it works:
In main window I have a list of items, when I click on one, it opens another window where I can modify it. When I close that window, I want main window to refresh its content. I've tried many event handlers including GotFocus() but it doesn't want to launch my method to refresh the list. Any advise?
If you want something to happen when the other window is closed, you can subscribe to its closed event. This will fire when the windows is closed.
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window1();
wnd.Closed += wnd_Closed;
wnd.Show();
}
void wnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
}

wpf application c# How to minimize/bring to front sub forms

My WPF application is a media player that has a button that launches another window to a certain location and is my webbrowser. Problem is, once i click my button to launch the browser, my 'hide' button does not hide the browser window :/, from what i researched i need to inherit button click events, take a look at the code:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
//opens a web browser if 'Web' is clicked on the WPF Media PLayer
Window1 f1 = new Window1();
f1.Show();
}
the ideas i thought would work is create a 'toggle button' like such:
private void button4_ClickWeb(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if ((bool)tb.IsChecked)
{
f1.Show();
//or
f1.WindowState = WindowState.Normal;
}
else
{
f1.Hide();
f1.WindowState = WindowState.Minimized;
}
}
this approach failed, and yes i tried variations and moving things around with NO success, i was told that i need to make a class of f1 to inherit button click events but i have no idea how to do that :/ any ideas?
here is how the app looks like:

XAML enable/disable view from another view

I'm a beginner and having some dicciculties with XAML.
I have a main view A, which has a button to open a pop-up-window B. When this happens, Window A should still be visible and openened, but disabled. I've done this in code behind (maybe not the cleanest way, but the only way I know how). The code i used for this is the following:
//Code behind from view A
private void X-Button_Click(object sender, RoutedEventArgs e)
{
var BWindow = new BView();
BWindow.Show();
this.IsEnabled = false;
}
I would like to get window A enabled again once i close window B, but i can t seem to get this work. Any help would be very much appreciated.
You could do it in the following way.
You register yourself on the Closed event of the window, and when it gets closed, you unregister the event, and re-enable the this form.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window BWindow = new BWindow();
BWindow.Show();
BWindow.Closed += BWindow_Closed;
this.IsEnabled = false;
}
void BWindow_Closed(object sender, EventArgs e)
{
Window win = sender as Window;
if (win != null)
{
win.Closed -= BWindow_Closed;
}
this.IsEnabled = true;
}
I assume you are looking for modal window. See similar question asked here: How do make modal dialog in WPF?
The solution is in using ShowDialog method from Window class. See here for reference:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Modal window is the concept when you open new window B from the existing window A. While the B is open, the A is disabled and cannot be used. Window A become active only when the B is closed.

How to effect change from child to parent window

This is probably a fairly simple question, but I have had no luck researching it thus far. I have a child window that has a yes and no button on it. When I click no I would like a check box to become unchecked in the parent window (which is the main window of my program).
Is there anyway that I could do something like?:
//No Button
private void No_Click(object sender, RoutedEventArgs e)
{
NameOfParent.checkBox.Checked = false;
}
I've seen this question but do not think that it exactly addresses my problem.
What is the correct way to go about this?
I've been using this to open my other windows:
Parent Window - Current code:
//Open new window
private void checkBox5_Checked(object sender, RoutedEventArgs e)
{
var newWindow = new ChildWindow();
newWindow.button_no.Click += buttonNo_click;
newWindow.Show();
}
//Unchecks Checkbox 5
private void buttonNo_click(object sender, RoutedEventArgs e)
{
checkBox5.IsChecked = false;
}
Opening the child window from the parent window:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.buttonNo.Click += buttonNo_Click;
window.Show();
}
In the code of the parent window, include this click event for the child window's no button:
private void buttonNo_Click(object sender, RoutedEventArgs e)
{
//code to uncheck the checkbox goes here
}
This turned out even simpler than the solution for Windows Forms that I've been using...and it appears to work in Windows Forms as well.
On construction of the child window, simply add a parameter that is of the type of the parent window.
Then when you are constructing your child window in the parent window code do the following (In The Parent Window Class):
ChildWindow child = new ChildWindow(this, other_param, other_param2.....);
ChildWindow.Show();
The keyword this will pass the parent window into the ChildWindow
Then you can store it into a class variable and access properties of it.
Child class constructor and parentWindow variable:
private ParentWindowType m_parentWindow = new ParentWindowType();
public ChildWindow(ParentWindowType parent, int other_param, string other_param2....)
{
m_parentWindow = parent;
}
Then in other child class methods, such as your button click handler you can access properties from the parent window:
public void ButtonClickHandler(....)
{
m_parentWindow.checkBox1.Enabled = true;
m_parentWindow.checkBox1.Checked = true;
}
This way you can store the parent window within the child window, and always have access to it.
Its nice and clean and simple this way. Ensure to make the parent window variable private in the child window class.
If made public, it would allow you to do funny things like
parentWindow.childWindow.m_parentWindow.childWindow etc......

Categories

Resources