Skip to main content

Microsoft Silverlight

Answered Question "Button" can not accept "MouseLeftButtonDown" messageRSS Feed

(0)

Nenong
Nenong

Member

Member

21 points

15 Posts

"Button" can not accept "MouseLeftButtonDown" message

"Button" can not accept "MouseLeftButtonDown" /"MouseLeftButtonUp"message, why??

<UserControl x:Class="TerryLee.SilverlightDemo14.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="500" Height="240">
    <Canvas Background="#46461F">
        <Button
           MouseLeftButtonDown="OnMouseDown"
           MouseMove="OnMouseMove"
           MouseLeftButtonUp="OnMouseUp"
           Canvas.Left="50" Canvas.Top="50" 
           Width="160" Height="80">
        </Button>
        <TextBlock x:Name="TBShow" Text="111" Canvas.Left="0" Canvas.Top="0" Width="100" Height="100"/>
    </Canvas>
</UserControl>


void OnMouseDown(object sender, MouseButtonEventArgs e)
{
    TBShow.Text = "OnMouseDown";
}
void OnMouseMove(object sender, MouseEventArgs e)
{
}
void OnMouseUp(object sender, MouseButtonEventArgs e)
{
    TBShow.Text = "OnMouseUp";
}

johnnystock
johnnystock

Contributor

Contributor

2295 points

362 Posts

Silverlight MVP
Answered Question

Re: "Button" can not accept "MouseLeftButtonDown" message

In Beta 2 the Mouse:eftButtonDown event was removed from the button and similar controls since they also had the more appropriate Click event.  If you change to Click you should be fine.

John Stockton
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

Skute
Skute

Member

Member

222 points

156 Posts

Re: "Button" can not accept "MouseLeftButtonDown" message

The ToggleButton class can be used to switch between 2 different states if that's what you need.

 

Nenong
Nenong

Member

Member

21 points

15 Posts

Re: "Button" can not accept "MouseLeftButtonDown" message

Oh, thank you~ ~.

Before I don't know this event is moved away.

Dastei
Dastei

Member

Member

49 points

20 Posts

Answered Question

Re: "Button" can not accept "MouseLeftButtonDown" message

 You can override this behavior and define your own button that fired mousedown and mouseup

 

 

    /// <summary>
    /// Quick hack to remove event handling that mark events as handled
    /// </summary>
    public class Button : System.Windows.Controls.Button
    {
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            e.Handled = false;
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            e.Handled = false;
        }
    }
  

Regards,
David Steinkopff

RichardP
RichardP

Member

Member

8 points

6 Posts

Re: "Button" can not accept "MouseLeftButtonDown" message

Switching to the click event only works if you don't need to get the mouse position in your click event.  I know you can capture the mouse move and record the position but there are cases such when the user might not move there mouse before clicking your button.  The example that comes to mind is if a combobox is open and you move your mouse over the top of the button to be clicked.  The first click closes the combobox and the second click would trigger the click event of the button without ever recording the mouseposition in the mouse move event.   MSFT needs to let us have the MouseLeftButtonDown back in the final release.

lexin
lexin

Member

Member

2 points

1 Posts

Re: "Button" can not accept "MouseLeftButtonDown" message

I install the Silverlight 2.0 release. Deaf on this event also.  Dunno when it will be back and whether it can be back.

StefanWick
StefanWick

Contributor

Contributor

2864 points

438 Posts

Microsoft

Re: "Button" can not accept "MouseLeftButtonDown" message

This behavior is by design and the solution was already posted in this thread: If you want to handle the MouseLeftButtonDown event on a button you can derive from button and handle the event there (or just unmark the 'Handled' flag).

Thanks, Stefan Wick

Microsoft Silverlight

Microsoft Silverlight | http://blogs.msdn.com/swick/
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities