WPF RoutedEventHandler doesnt work with content

292 views Asked by At

I have problem with c# WPF routedevent. This code works fine:

        MyLabel myLabel = new MyLabel();//MyOwn component
        myLabel.Oma += new RoutedEventHandler(myLabel_Click);
        GameArea.Children.Add(mylabel);//GameArea is UniformGrid

But when I put myLabel in ToggleButton's content, routedeventhandler(myLabel_Click) doesnt catch the Oma-event(I debugged that):

        MyLabel myLabel = new MyLabel();//MyOwn component
        myLabel.Oma += new RoutedEventHandler(myLabel_Click);
        System.Windows.Controls.Primitives.ToggleButton box = new System.Windows.Controls.Primitives.ToggleButton();
        box.Content = myLabel;
        GameArea.Children.Add(box);//GameArea is UniformGrid

So why do ToggleButton blocks my routedevent?

EDIT: Code works fine, but it makes troubles, when I put myLabel in ToggleButton's content.
OmaEvent in MyLabel seems like this:

public static readonly RoutedEvent OmaEvent =
EventManager.RegisterRoutedEvent("Oma", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyLabel));

    public event RoutedEventHandler Oma
    {
        add { AddHandler(OmaEvent, value); }
        remove { RemoveHandler(OmaEvent, value); }
    }

    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        CaptureMouse();
    }

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        base.OnMouseUp(e);
        if (IsMouseCaptured)
        {
            ReleaseMouseCapture();
            if (IsMouseOver)
                RaiseEvent(new RoutedEventArgs(OmaEvent, this));
        }

    }

OmaEvent never raise, if I put it inside the ToggleButton. If I don't put it inside the ToggleButton, it works.

2

There are 2 answers

0
Mike Strobel On BEST ANSWER

The Oma event is not being raised because the parent ToggleButton steals the mouse capture in its ButtonBase.OnMouseLeftButtonDown method, which gets invoked after your OnMouseDown method. No other element will receive mouse events as long as the ToggleButton has mouse capture, so your label will not receive the MouseUp event, and your OnMouseUp method will not be called.

Set e.Handled = true; in your OnMouseDown method to prevent the event from bubbling up to the ToggleButton. This will prevent it from stealing focus, though it will also prevent the button from working normally when the MyLabel within is clicked. It's unclear what behavior you want in this scenario.

0
landoncz On

It sounds like your RoutingStrategy for your custom Oma event may not match with what you're trying to do.

If the Oma event is a RoutedEvent set to Direct, and the MyLabel class is of type UIElement then you should be able to do something like the following:

GameArea.Children.Add(box);//GameArea is UniformGrid
GameArea.AddHandler(Oma, new RoutedEventHandler(myLabel_Click));