• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Advice requested - why doesn't this eventhandler get called?

J

jsnover13

#1
I'm missing something simple.
I want to hand craft my object hierarchy in code but I'm doing
something wrong. This code displays properly but it does the
MouseDown eventhandler never gets called.

Any ideas?

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Button b = new Button();
b.Content = "PUSH ME";
b.MouseDown += new MouseButtonEventHandler(b_MouseDown);
this.Content = b;
}

void b_MouseDown(object sender, MouseButtonEventArgs e)
{
Button b = (Button)sender;
b.Content = "Down";
}
}
}
 

My Computer

M

Mark Salsbery [MVP]

#2
<[email protected]> wrote in message
news:[email protected]

> I'm missing something simple.
> I want to hand craft my object hierarchy in code but I'm doing
> something wrong. This code displays properly but it does the
> MouseDown eventhandler never gets called.
>
> Any ideas?

MouseDown is being handled by the Button class.

You could instead handle the tunneling PreviewMouseDown event instead,
or maybe use a Button-derived class if appropriate:

public class MyButton : Button
{
protected override void OnMouseDown(MouseButtonEventArgs e)
{
this.Content = "Down";

base.OnMouseDown(e);
}
}


By the way, a better place for WPF questions is
http://social.msdn.microsoft.com/forums/en-US/wpf/threads/
This newsgroup is pretty dead :)

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


>
> using System;
> using System.Windows;
> using System.Windows.Controls;
> using System.Windows.Input;
> namespace WpfApplication1
> {
> public partial class Window1 : Window
> {
> public Window1()
> {
> InitializeComponent();
> Button b = new Button();
> b.Content = "PUSH ME";
> b.MouseDown += new MouseButtonEventHandler(b_MouseDown);
> this.Content = b;
> }
>
> void b_MouseDown(object sender, MouseButtonEventArgs e)
> {
> Button b = (Button)sender;
> b.Content = "Down";
> }
> }
> }
 

My Computer