Our application uses ChildWindow modals all throughout. I am in need of a way to detect when these ChildWindows are being opened and closed in order to do some UI trickery (don't ask and not important).
Alternatively, since the modal ChildWindow "grays out" and disables the app in the background while open, is there someway to hook into that event?
Lately, I've tried GotFocus and LostFocus events on the main LayoutRoot, but it's proven to be erratic. Any ideas would be greatly appreciated.
ChildWindow has a Closed event, so you can have some code run when it is closed, e.g.:
var window = new MyChildWindow();
window.Closed += (s, e) => DoSomethingWhenWindowCloses();
window.Show();
Related
I have a third-party image control inside an SL5 listbox itemtemplate. This makes for a nice scrollable gallery of images.
Now for the trouble: the third-party image control (LeadTools v17.5) has an interactive feature wherein mouseleftbuttondown causes a draggable magnifying glass to appear. This works great when the control is not hosted in a listbox. But clicking on the control within a listboxitem does nothing. After some research I "believe" this is because the listboxitem is trapping the mouseleftbuttondown event marking it as handled so the image control never sees it. In my application I have no need to handle the mouseleftbuttondown event at the listbox level (other buttons etc control my UI). Assuming I'm correct, is there a way to stop the listboxitem from listenting to this event?
Or perhaps I'm completely wrong about the cause. In that case any other ideas about why the listbox appears to block mouseleftbuttondown events from reaching the controls within is appreciated.
Thanks,
Mark
Instead of trying to keep the ListBoxItem from handling the event, you might be able to use UIElement.AddHandler with handledEventsToo: true, if you can get the necessary UIElement and Delegate references to trigger the image control's feature.
Thanks for your suggestions. In this case it turns out the quick solution was to add this handler to the image control:
private void leadGalleryImageViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
e.Handled = true;
}
From this I gather the mouseleftbuttondown event was being received by the image control all along, but likely as it bubbled up through the listitem and beyond, the listitem did its thing then marked it as handled effectively killing anything the image control was trying to do. By marking the event as handled at the image control level, the listitem ignores it.
When my modal ChildWindow closes, the parent control does not get "un-grayed" and all user input is blocked, forcing me to restart the application. Exact scenario which causes this behavior consistently:
ChildWindow A pops up and user clicks a custom button on the bottom of the window (instead of OK or Cancel). My code does some work and then calls the Close() method for the ChildWindow.
ChildWindow A closes and functionality is restored to parent control (controls are un-grayed).
User causes ChildWindow B to pop up. User clicks system-generated OK or Cancel button.
ChildWindow B closes, but the parent controls are still grayed out and inaccessible.
Either of the windows work fine repeatedly on their own. Any thoughts?
I saw something similar (it might not fix your exact problem) and found some discussion about the ChildWindow here
they suggested this method in the ChildWindow Closed event and it worked for me.
Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
Also try calling this.DialogResult = true instead of the Close method.
I need to fire some event when WPF button is pressed (by mouse, keyboard, touchscreen, etc) and to fire event when WPF buttons is unpressed.
How to do this? It should be easy but I can't find how to do this.
You can derive from Button and override the OnIsPressedChanged method and fire a custom event there.
Or you can bind to the ButtonBase.IsPressed property.
Another option is to use DependencyPropertyDescriptor:
var descriptor = DependencyPropertyDescriptor.FromProperty(Button.IsPressedProperty, typeof(Button));
descriptor.AddValueChanged(this.button, new EventHandler(IsPressedChanged));
If you are using MVVM, you can use event triggers to solve your problem. This way, you can still separate your UI requirements from your application logic.
Example 1
Example 2
I have a Silverlight game controlled by the keyboard, and I want it to go into pause when it loses the keyboard focus (e.g. the user clicks on another part of the hosting webpage, or moves to another browser tab).
I used to do this in Silverlight 1.1 by subscribing to the LostFocus event on my RootVisual UserControl, but in the last two versions of Silverlight, I have found this event seems to fire unexpectedly shortly after clicking a button in my application (in Silverlight 2 it fired once, in Silverlight 3 twice!).
Is there a way in javascript on the hosting page, or within Silverlight to detect loss of focus more reliably?
I finally found a solution to this problem. The RoutedEventArgs property on the LostFocus event has an OriginalSource property which allows me to ignore any LostFocus events that come from children of the RootVisual.
void Page_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource == this)
{
Pause();
}
}
I have a Window instance which I show by calling wInstance.ShowDialog() from a button click and I close the window by pressing Alt+F4. The issue now is that I cant call wInstance.ShowDialog() again. How can I re-use the same window instance again.
Exception :
Cannot set Visibility or call Show or ShowDialog after window has closed.
You need to override the wInstance OnClosing method to set the window visibility to hidden and cancel the close event.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
this.Visibility = Visibility.Hidden;
e.Cancel = true;
}
What exactly is it that makes it so important to use the same window?
If you are using MVVM, you could just reuse the viewmodel for a new window.
I'm reusing a window as a Dialog that uses a treeview and the client wants the tree branches to remain open for a more selections.
The override worked for re-use, and the branches stay expanded.
I'm not using a view model to keep it simple as it is a read only selection dialog. But since I can't seem to clear the selection yet, I may have to switch to a view model.