I am writing an application using WPF. I would like such an action: when I press down mouse button on a button, another window appears, when I release mouse button wherever, this window hides. That is my code so far:
XAML:
<Button Margin="0,0,0,0" Name="button_wykres" PreviewMouseUp="button_wykres_PreviewMouseUp" PreviewMouseDown="button_wykres_PreviewMouseDown">
C#:
private void button_wykres_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
omww.Top = this.Top+50;
omww.Left = this.Left +180;
omww.Show();
}
private void button_wykres_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
omww.Hide();
}
The problem is that new window (omww) appears under the main one, but I need it on top. When I tried other events, i.e. button.Click window is showed above the old one, as I want, and that confuses me. However, Click event doesn't meet my needs. I'd be grateful if anybody help me.
As I see it, you have two possible options for making the child Window appear on top of the parent Window. The first was mentioned by #Viv in a comment and that is to set the TopMost property on the child Window to true:
omww.Topmost = true;
The second option would be to set the Owner property of the child Window to the parent Window (if there is a direct relationship between them):
omww.Owner = this; // if called from the parent Window code behind
Related
I would like to display a kind of window when I move the mouse over a ListView Object. When the mouse leaves the object, the window should close again.
Does anyone have some tips for me?
Of course that did not work that way. In addition, the window should go to the mouse and not somewhere.
Test:
private void ListViewBilling_MouseEnter(object sender, MouseEventArgs e)
{
_billingInfoWindow = new BillingInfoWindow();
_billingInfoWindow.ShowDialog();
}
private void ListViewBilling_MouseLeave(object sender, MouseEventArgs e)
{
_billingInfoWindow.Close();
}
The window closes and opens continuously.
How do I get a window (popup) to be displayed only when the mouse is moved over a ListView object? Not everywhere in the ListView.
You should use ToolTip or Popup controls. Details here:
WPF ToolTips - MSDN
WPF Popups - MSDN
Understand the differences and choose the best you need. Main differences are:
ToolTips should not be hoverable, just some extra information
Popups are separate windows, they can get custom, clickable items (eg. buttons)
Popups can be opened on hover as well.
What is happening is that the dialog box opens which causes the mouse to leave the billing window which causes the dialog box to close. Then the mouse re-enters the billing window which opens the dialog box, etc., etc. This is causing the loop you are seeing.
I suggest that you adjust and have the dialog box close when the mouse leaves the dialog box.
My current solution looks like this:
private void Show_PopupBillingPreview(object sender, MouseEventArgs e)
{
var listViewItem = e.Source as ListViewItem;
var billing = listViewItem?.Content as Billing;
PuBillingPreviewTitle.Text = billing?.BillingId.ToString();
PuBillingPreview.PlacementTarget = listViewItem;
PuBillingPreview.Placement = PlacementMode.MousePoint;
PuBillingPreview.IsOpen = true;
}
private void Hide_PopupBillingPreview(object sender, MouseEventArgs e)
{
if (PuBillingPreview.IsOpen)
PuBillingPreview.IsOpen = false;
}
Now I have the problem that I have a flicker when the popup window appears directly under the mouse.
I am using following code in C# to add a Button
Button TextLabel = new Button(); //local variable
TextLabel.Location = new Point(0, 0);
TextLabel.Visible = true;
TextLabel.Enabled = true;
TextLabel.AutoSize = true;
TextLabel.Click += click;
this.Controls.Add(TextLabel);
And its click handler is
protected void click(object o, EventArgs e)
{
MessageBox.Show("hello");
}
Though the Button is visible and responding to mouse hover, but nothing is happening on its click. What could be wrong or missing?
If I write this same code in an independent project, it works!!!!! Strange. but why????
Form Properties: (if required)
1. Show in taskbar: false
2. Borderless
3. 50% Opaque
Today I realised that just registering click event for a control will not make any event to work unless its parent (in my case its form) on which that control is still active.
Parent control will receive event notification earlier than its child controls. This is a simple and obvious observation, but if not paid attention will make undesirable effects.
That's the mistake I did, I made another form active on my form activated event, hence any control in it didn't received events like mouse clicks.
Talking of 'hover effects are working', then yes, even if a form is inactive, hover works.
So I just removed the line of code that made another form active and everything is working fine now.
private void Form1_Activated(object sender, EventArgs e)
{
//if (form2!=null) form2.BringToFront(); //commented this
}
I'm working in Silverlight, but potentially a WPF solution would work as well.
My problem is very simple. I have lots of modal Child Windows that can be open, and in their generic menu is a home button. This button is supposed to close all of the child windows and return to the base screen. I have a few different types of 'generic child windows' that host lots of different UserControls, so by far the easiest way to implement this is to, when the window comes into focus, check if the global ReturnToHome bool is true, and if it is, just close it.
I've tried all of these
private void ChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_MouseEnter(object sender, MouseEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
The issue is, GotFocus doesn't fire until I actually click on the window. MouseEnter is a little better, but doesn't fire until I move the mouse. IsEnabledChanged never fires because the Child Window doesn't disable anything. Checking every child window when it closes to see if Home has been clicked isn't easy because of the sheer number of places where you can open child windows, and several of them are nested within User Controls where I couldn't even easily access DialogResult. Any idea how I could do this?
Also I should note that I want each of the windows to close one by one, from top down, because each window that closes does its own verification to see if it should warn the user before closing (giving the user the option to cancel closing)
TopMost is a bool property which is either set to true or false and as far as I'm aware, there is no public property like Z-Index that will tell you the order in which your Windows were set to TopMost. However, there is a simple solution... just maintain a static int variable that will register this order. Each time you add a new Window, set the number into its Tag property:
Window childWindow = new Window();
childWindow.Tag = currentWindowNumber++;
...
childWindow.ShowDialog();
Then, when you want to close them in order, you can just do something like this:
foreach (Window window in Application.Current.Windows.OfType<YourWindowType>()
.OrderBy(w => (int)w.Tag))
{
((AnimationWindow)window).CloseWindow();
}
I'm working on this WPF project. The main window has 8 buttons which all open another window. What I'd like to do, is when the user clicks one of the buttons, the main window is hidden(not closed), and the secondary window is open. Now, when the secondary window is closed, I want to unhide my main window. Here's the code I've got.
public WndwProjectSetup(Window mainWindow)
{
InitializeComponent();
_mainWindow = mainWindow;
_mainWindow.Visibility = Visibility.Hidden; // hides main window
}
private void WindowClosing(object sender, CancelEventArgs e)
{
_mainWindow.Visibility = Visibility.Visible; // unhides main window
Close(); // close Project Setup window
}
This seems simple and straight forward to me. Yet, I get this error:
Cannot set Visibility to Visible or call Show, ShowDialog, Close, or
WindowInteropHelper.EnsureHandle while a Window is closing.
My question is, why is this not acceptable? What do I need to look into to figure out how to do this?
You don't need to call Close, it's already closing. (If anything you can cancel it by setting e.Cancel to true)
I have set the Visibility property of the main window to Hidden and added the following in Window_Loaded:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
}
But it doesn't show up the Window. Any specific reason for this?
The window is not loaded until it is shown, as per your code it will not be shown until it is loaded. Obivously this cannot work like that, right?
I was having an issue with this as well and it seems changing the visibility alone on a main window doesn't work as H.B. pointed out. For my case, I wanted to not show the window until it was completely loaded and was able to achieve that by using the property I've linked to here, along with the Show() and Hide() functions on the Window object.
System.Windows.Window.ShowActivated
When initializing the window object don't set visibility to hidden, instead follow the next steps
Set the ShowActivated property to false this.ShowActivated = false;
Call the Hide() function on the window object this.Hide();
On your window loaded function from your original example call this.Show();
It is also possible in some WPF apps for the this reference not to work as expected, however if this is the case go to the XAML and find the name property of the window. You should be able to ref the window from the code via that name. Ex.
<Window x:Name="MainWindow">
//Code Behind Below
MainWindow.ShowActivated = false;