MouseLeftButtonUp, MouseLeftButtonDown Not raised by button in UserControl hosted in RadTabItem

3 posts, 1 answers
  1. Jeff Bloom
    Jeff Bloom avatar
    2 posts
    Member since:
    Aug 2009

    Posted 04 Sep 2009 Link to this post

    Is there a bug where MouseLeftButtonUp and Down are not triggered when there is a user control inside of a RadTabItem?  Is something intercepting and handling the click before the button gets it and is there a workaround? The enter and leave events seem to be making it to the button.  This is kind of a deal breaker if this functionality does not work.

    Here is the code I used to illustrate the bug:

    UserControl XAML:
    <UserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"  x:Class="RadTabTest.ControlWithButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />            
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.5*" />
                <ColumnDefinition Width="0.5*" />
            </Grid.ColumnDefinitions>
        <Button x:Name="btnTest" MouseLeftButtonDown="btnTest_MouseLeftButtonDown" MouseLeftButtonUp="btnTest_MouseLeftButtonUp" Content="Click Me" MouseEnter="btnTest_MouseEnter" MouseLeave="btnTest_MouseLeave"></Button>
            <dataInput:Label x:Name="lblTest" Grid.Column="1" Content="This is a label" />
        </Grid>
    </UserControl>

    UserControl CS:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    namespace RadTabTest
    {
        public partial class ControlWithButton : UserControl
        {
            public ControlWithButton()
            {
                InitializeComponent();
            }

            private void btnTest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                lblTest.Content = "MouseLeftButtonDown";
            }

            private void btnTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                lblTest.Content = "MouseLeftButtonUp";
            }

            private void btnTest_MouseEnter(object sender, MouseEventArgs e)
            {
                lblTest.Content = "MouseEnter";
            }

            private void btnTest_MouseLeave(object sender, MouseEventArgs e)
            {
                lblTest.Content = "MouseLeave";
            }
        }
    }

    The XAML for the Tab Host for the UserControl:

    <UserControl xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"  x:Class="RadTabTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
      <Grid x:Name="LayoutRoot">
      <telerikNavigation:RadTabControl x:Name="tcTest"></telerikNavigation:RadTabControl>
      </Grid>
    </UserControl>

    The CS for the Host:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;

    namespace RadTabTest
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                RadTabItem ti = new RadTabItem();
                ti.Header = "test";
                ti.Content = new ControlWithButton();
                tcTest.Items.Add(ti);
                
            }
        }
    }

  2. Answer
    Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 04 Sep 2009 Link to this post

    Hi Jeff,

    The same behavior is present if you put the UserControl outside the TabControl as well.

    This i because the Button handles the MouseDown and MouseUp events because it raises  the Click event and it is expected that it will be used instead of the other two events.

    If you still want to handle these events you have to use the AddHandler method that has an overload where you can explicitly specify whether you want to handle handled events, in your case like so:

    btnTest.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnTest_MouseLeftButtonDown), true);

    This is standard behavior for the routed events that have been handled.



    Best wishes,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Jeff Bloom
    Jeff Bloom avatar
    2 posts
    Member since:
    Aug 2009

    Posted 04 Sep 2009 Link to this post

    That did the trick...thanks!  I guess I don't quite understand why the Up and Down mouse events aren't bubbled up by default.  I'll use the click handler instead since in this case I don't care if the person is holding down the mouse left button over the button.

    Thanks again!
Back to Top