How is WPF mouse event wired? - wpf

I am WPF newbie. In a WPF sample code I am looking at, there is a mouse event handler as follows:
namespace Recipe_04_15
{
public class DragCanvasControl : Canvas
{
...
static DragCanvasControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DragCanvasControl),
new FrameworkPropertyMetadata(typeof(DragCanvasControl)));
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
...
}
...
What I don't understand is how the OnPreviewMouseLeftButtonDown method is wired to the mouse button down event. The XAML code does not have events specified?

The function is already wired in the UIElement class.
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmouseleftbuttondown.aspx
Since you inherit from the canvas you also inherit also from UIElement somewhere deeper.
DragCanvasControl => Canvas => Panel => FrameworkElement => UIElement
By overiding the OnPreviewMouseLeftButtonDown from UIElement you get access to this event.

If it is not in the XAML, it has to be attached in the code-behind. If it is not, the method will not be executed.
Be aware that you are actually looking for the PreviewMouseLeftButtonDown event and not some Click or MouseDown.

Related

WPF UserControl GotFocus

I have a UserControl that contains several ComboBoxes and Buttons.
In the window that hosts the UserControl I need to update some properties when the UserControl gets the focus.
My problem is, that every time, when the focus inside the user control changes, that get a GotFocus event in the hosting windows.
Is there some kind of best practice to make sure, that I only get one GotFocused event in the hosting window? I mean, if I step through controls inside the UserControl the focus is always inside the UserControl, so I don't want a GotFocused event.
This is the solution I came up to:
First of all this post was the key for my solution: WPF UserControl detect LostFocus ignoring children .
And Refer to active Window in WPF? .
Using the functions in these posts I registered the LostFocus event in my UserControl.
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
var focused_element = FocusManager.GetFocusedElement(Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive));
var parent = (focused_element as FrameworkElement).TryFindParent<KeywordSelector>();
if (parent != this) userControlHasFocus=false;
}
And then ...
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
if (userControlHasFocus == true) e.Handled = true;
else userControlHasFocus = true;
}
This way I keep track of the focus. userControlHasFocus is false be default. When the GotFocus() happens for the first time it's false and the GotFocus event is not stopped form bubbling up. But userControlHasFocus gets set to true, because now the focus is inside the UserControl.
Whenever the focus moves to another control, LostFocus checks if the new controls parent is the UserControl. If not, it resets the userControlHasFocus to false.

Bubble mouse event from WPF to WinForms

I have WPF control hosted inside a WinForms control using ElementHost. The WinForms control has a context menu. I want to show the context menu when user right click on the WPF control. How can this be done? It seems mouse event is not bubbled from WPF to WinForms.
it is not automatically bubbled up, as you might have handled it in the WPF control in the first place. However, you can easily add this yourself.
In your WPF user control, expose an event that you trigger on right mouse up:
public event Action ShowContext;
private void rectangle1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (ShowContext != null)
{
ShowContext();
}
}
Then in your winforms control with element host you can use it like so:
public UserControl1 WpfControl { get; set; }
public Form1()
{
InitializeComponent();
WpfControl = new UserControl1();
WpfControl.ShowContext += () => contextMenuStrip1.Show(Cursor.Position);
elementHost1.Child = WpfControl;
....

numericUpDown ValueChanged after MouseLeftButtonDown

I have 2 controls on a form. One numericUpDown (from the Silverlight Toolkit) and a simple Rectangle.
On the MouseLeftButtonDown of the Rectangle I popup a MessageBox with the numericUpDown value.
If I use the arrows to change the value of the numericUpDown, everyting is fine. But if I edit the value manually (with the keyboard) and immediately click on the Rectangle it shows the previous value of the numericUpDown. If I click a sencond time on the rectangle it will show the new value.
The numericUpDown.ValueChanged event is raised after the Rectangle.MouseLeftButtonDown event.
Is that a Silverlight bug? Anybody knows a workaround for that?
(btw I cannot change my Rectangle controls or events)
As workaround I propose you to create your own control like:
public class MyNumericUpDown : NumericUpDown
{
private TextBox _textBox;
public void Sync()
{
ApplyValue(_textBox.Text);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_textBox = (TextBox)GetTemplateChild("Text");
}
}
Now you can use method Sync to syncronize display text with control Value property. You can call method from XAML declaratively or in code behind. In your case:
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
myNumericUpDown.Sync();
MessageBox.Show(myNumericUpDown.Value.ToString());
}

Expose a Click event of a button inside a UserControl in Silverlight

I have a button inside my UserControl. I have three instances of this UserControl on the same page.
How can I expose the click event of the button inside such that I can assign different events for each instance of my UserControl.
I think this is similar to concept behind exposing DependencyProperty but I don't understand how to do it for events.
Thanks.
I normally add an event of the same name (and same parameters) to the user control and subscribe to the child control's original event, so I can pass the event on:
public partial class ClickEventControl : UserControl
{
public event EventHandler<RoutedEventArgs> Click;
public ClickEventControl()
{
InitializeComponent();
}
private void aButton_Click(object sender, RoutedEventArgs e)
{
if (Click != null)
{
Click(sender, e);
}
}
}
I would also be interested if there is a more general way of doing it.

No events passed to WPF adorner layer

I am trying to make a nice "drag and drop zone" in WPF that is displayed in the adorner layer when something is being dragged into the main application. The problem is that I do not get any events from my adorner, even though it according to documentation should receive all input events since it is in a higher z-order.
To debug my problem I created a really simple example where I have a user control with only a button in it. This user control is displayed in the adorner layer, but I cannot click the button. Why? What have I done wrong?
My adorner class is constructed like this:
public ShellOverlayAdorner(UIElement element, AdornerLayer adornerLayer)
:base(element)
{
_adornerLayer = adornerLayer;
_overlayView = new AdornedElement();
_overlayView.AllowDrop = true;
_adornerLayer.Add(this);
}
and is created in the main window by
private void Window_Loaded(object sender, RoutedEventArgs e)
{
adornerLayer = AdornerLayer.GetAdornerLayer(MyTopGridWithButtonInIt);
ShellOverlayAdorner shell = new ShellOverlayAdorner(MyTopGridWithButtonInIt, adornerLayer);
}
I do not get any events at all from my control, i.e. no mouse clicks, mouse over, button clicks. I cannot even click the button in the adorner layer. What have I done wrong?
I don't know if you already tried that:
If you want the element added to react to events, I think that the element must be bound to the visual tree of the adorner.
The way to do it is to use a VisualCollection, intitialized to the adorner itself, or at least, this way it seems to be working:
VisualCollection visualChildren;
FrameworkElement #object;
public CustomAdorner(UIElement adornedElement) :
base(adornedElement)
{
visualChildren = new VisualCollection(this);
#object = new Button {Content = "prova"};
visualChildren.Add(#object);
}
protected override Visual GetVisualChild(int index)
{
return visualChildren[index];
}
This way the events are correctly routed.
I just had the same issue. Following the advice from MSDN sorted it for me:
Adorners receive input events just
like any other FrameworkElement.
Because an adorner always has a higher
z-order than the element it adorns,
the adorner receives input events
(such as Drop or MouseMove) that may
be intended for the underlying adorned
element. An adorner can listen for
certain input events and pass these on
to the underlying adorned element by
re-raising the event.
To enable pass-through hit testing of
elements under an adorner, set the hit
test IsHitTestVisible property to
false on the adorner.
i.e In the adorner itself, make sure IsHitTestVisible = false

Resources