locked
MouseWheel event not firing for Canvas in WPFUserControl hosted in ElementHost in Windows XP RRS feed

  • Question

  • I have a Canvas in a WPFUserControl hosted in an System.Windows.Forms.Integration.ElementHost. I add a MouseWheel event to the Canvas in code behind like this:

     

    BaseCanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(m.BaseCanvas_MouseWheel);

     

    The event to catch the event is:

     

    public void BaseCanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args)

    {

    ...

    args.Handled = true;

    }

     

    But I never get the event fired, when I rotate the mouse wheel with the mouse over the canvas. I notice, that in the MSDN documentation for UIElement.MouseWheel event it says: Platforms: Vista. Does this mean, that it doesn't work on Windows XP? Or what else can be the reason for the event not to fire?

    Friday, April 25, 2008 10:42 AM

Answers

  • So, I build the same scenario you've got:

    A windows- form window with an ElementHost. In the ElementHost is a WPF- Usercontrol which is just a Canvas (and nothing else in my case)

     

    I did the registration of the eventin the Contstructor of the UserControl. The function which should be called, I've added to the UserControl itself.

     

    and... it worked ...

     

    UserControl XAML

    <UserControl x:Class="WpfApplication1.UserControl1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="300" Width="300">

    <Canvas Name="BaseCanvas" Background="AliceBlue">

    </Canvas>

    </UserControl>

     

     

    UserControl (Code behind)

    public partial class UserControl1 : UserControl {

    public UserControl1() {

    InitializeComponent();

    BaseCanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(BaseCanvas_MouseWheel);

    }

    public void BaseCanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args) {

     

    args.Handled = true;

    }

    }

     

     

    There's just one little difference: I've got the function directly in the UserControl (you've got it in "m.BaseCanvas_MouseWheel"), but I don't think that's the reason.

     

    When do you register your event. Are you sure that it is called?

     

    Is there any other difference?

     

    By the way, I'm also using XP

    Tuesday, April 29, 2008 6:39 PM

All replies

  • I guess you haven't set any background- color to your canvas, is it?

     

    Every panel (Grid, Canvas, ...) need a backgroundcolor to get mouse- events handled.

    You can set it for example to transparent so you won't see an difference, but the event will be fired afterwards ;-)

    • Proposed as answer by TechNoHick Sunday, August 8, 2010 3:18 PM
    Sunday, April 27, 2008 6:17 PM
  • Yes, I do have a Background color on the canvas, so this can't be the problem

     

    Monday, April 28, 2008 12:09 PM
  • So, I build the same scenario you've got:

    A windows- form window with an ElementHost. In the ElementHost is a WPF- Usercontrol which is just a Canvas (and nothing else in my case)

     

    I did the registration of the eventin the Contstructor of the UserControl. The function which should be called, I've added to the UserControl itself.

     

    and... it worked ...

     

    UserControl XAML

    <UserControl x:Class="WpfApplication1.UserControl1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="300" Width="300">

    <Canvas Name="BaseCanvas" Background="AliceBlue">

    </Canvas>

    </UserControl>

     

     

    UserControl (Code behind)

    public partial class UserControl1 : UserControl {

    public UserControl1() {

    InitializeComponent();

    BaseCanvas.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(BaseCanvas_MouseWheel);

    }

    public void BaseCanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs args) {

     

    args.Handled = true;

    }

    }

     

     

    There's just one little difference: I've got the function directly in the UserControl (you've got it in "m.BaseCanvas_MouseWheel"), but I don't think that's the reason.

     

    When do you register your event. Are you sure that it is called?

     

    Is there any other difference?

     

    By the way, I'm also using XP

    Tuesday, April 29, 2008 6:39 PM
  • I actually register the event (BaseCanvas.MouseWheel += ...) in another code-behind file than the one associated with the WPFUserControl. I have tried to do it in the WPFUsercontrol.xaml.cs file and also have the event block in this file, but still I don't get the event triggered.

     

    The curious thing is, that I have long had a MouseDown event for the BaseCanvas define in exactly the same way and it is working fine.

    Wednesday, April 30, 2008 7:00 AM
  • You are really sure that the line is called where you do the registration? (Breakpoint on the line...)

     

    How can you subscribe to this event from outside your UserControl (or is your Usercontrol derived from Canvas?)

     

    Maybe you can post more of your code where it's geting clear where your are doing what exactly

    Wednesday, April 30, 2008 11:51 PM
  • I had the same problem and by setting the Background color it finally worked. My Question is why do you have to set the background color to get the mouse events to get handled?!  bizarre !
    • Proposed as answer by AdamCowls Tuesday, August 25, 2009 10:50 PM
    Friday, May 30, 2008 12:17 PM
  • I think it has something to do with the HitTesting. If the background color is set to {x:Null} the hit testing will go through the control. Try setting the background color to Colors.Transparent. I hope this helps.

    Christian
    Tuesday, February 3, 2009 12:04 PM
  • I had this same problem; A User Control with very little content (just a grid), and I was trying to get the Mouse Wheel Event (which would not work reliably at all). Putting in a Canvas and setting the background color worked. The background color only need be Transparent!

    <Grid x:Class="Example"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Width="300" Height="300" Focusable="True" Mouse.MouseWheel="Grid_MouseWheel"
        >
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
      </Grid.ColumnDefinitions>
      <Canvas Name="BaseCanvas" Background="Transparent"></Canvas>
      <ScrollBar Grid.Column="1" Name="Scroller" Width="18" Scroll="Scroller_Scroll" />
    </Grid>
    • Edited by AdamCowls Tuesday, August 25, 2009 10:54 PM added code example
    Tuesday, August 25, 2009 10:51 PM
  • Seems like setting Focusable="True" was the one thing that made the MouseWheel event start happening for me.
    Sunday, February 21, 2010 1:54 AM
  • I 've got exactly the same code and problem that original poster describes.


    I suscribe to MouseWheel event in my Canvas constructor, and I am absolutely sure that  the line  is called.

     

    I tried separately and together  the various solutions mentioned in ths thread:

     

    - I set the Canvas background to "transparent", or "White"

    - I set this.focusable = true for the Canvas

    -I performed the subscribtion out of Canva, it means directly in the user control

     

    And..  It does not work.

     

    What puzzles me is that I also use some other events, like MouseMouve, or MouseLeftButtonDown . I declare them exacly in the same way as MouseWheel, and for these events, everything works fine.

     

    Is that a bug  ?  ( I use VS2010)

     

     

     

     

    Monday, February 7, 2011 10:52 AM
  • I'm hitting the same problem. In my case, I was setting Canvas.set_RenderTransform. My polygons displayed nicely, but scrollwheel, hittesting, and setting the background (using a solid color brush) were all broken. I remove the call to Canvas.set_RenderTransform and instead manually applied the transform. Then it worked for me.  

    Instead of :

        _canvas.RenderTransform = t
        Point p = ...;
        // use p

    I did
        // _canvas.RenderTransform = t  // don't set this
        Point p = t.Transform(p)
        // use p

    And that worked.


    I even found that if mattered which I did first: set background via a solidcolor brush vs. applying the tranform. That just seems broken since a background should be transform agnostic.


    My guess is that there are bugs around hanlding transforms.


    http://blogs.msdn.com/jmstall
    Sunday, July 17, 2011 5:08 PM