Skip to main content
Home Forums Silverlight Programming Programming with .NET - General MouseLeftButtonDown
10 replies. Latest Post by GearWorld on May 25, 2009.
(0)
GearWorld
Participant
846 points
1,105 Posts
05-23-2009 7:42 AM |
Hi,
Any idea why this event is never triggered event tought everything is in place and no error reported
<GearObjects:Vista_MaximizeButton HorizontalAlignment="Right" Margin="0,-2.875,34.75,6.875" Width="48" x:Name="btMaximize" MouseLeftButtonDown="btMaximize_MouseLeftButtonDown"/>
Sergey.L...
Contributor
7250 points
1,351 Posts
05-23-2009 10:21 AM |
Can you show the code of Vista_MaximizeButton class?
MarkTap
1442 points
263 Posts
05-23-2009 12:28 PM |
Here are my two guesses:
-the obvious one: some part of the button is swallowing the mouse click before it gets to you (maybe it publishes a Click event instead)
-more subtle: the piece of the button you click on doesn't have a background color set
Has this worked for other people? I.e. is this button a component you purchased and generally works but for some reason it's not working for *you*? Or are you writing the button and this is the first time you've tested this part?
05-23-2009 6:39 PM |
I just took the xaml of a guy here in the forum that was the closest VISTA style CLOSE button and it is a Button tamplateAlso, I never saw the CLICK even anywhere when I did place the button on my form so I took the MouseLeftButtonDown
Here's my actual xaml a bit modified from the original but it's all the same I just resized a few thing and change some VSM
<UserControl 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" x:Class="GearObjects.Vista_CloseButton" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" d:DesignWidth="680" d:DesignHeight="504" Foreground="#00000000" BorderThickness="1,0,1,0" BorderBrush="#FFF8FF00"> <UserControl.Resources> <LinearGradientBrush x:Key="BarsBrush" EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF1F2A76" Offset="0.567"/> <GradientStop Color="#FF5066F7" Offset="0"/> <GradientStop Color="#FF082FE8" Offset="0.558"/> </LinearGradientBrush> </UserControl.Resources> <Button Content="x" Foreground="White" FontSize="14" FontWeight="Bold" Height="20" Width="50"> <Button.Template> <ControlTemplate TargetType="Button"> <Grid Width="36.875"> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualState x:Name="Normal"/> <vsm:VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="OuterBorder" Storyboard.TargetProperty="(Shape.Stroke).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/> </ColorAnimationUsingKeyFrames> <!--<DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF"/> </ColorAnimationUsingKeyFrames>--> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Pressed"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="InnerWhiteBorder" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFF3600"/> </ColorAnimationUsingKeyFrames> <!--<ColorAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#FF6DBDD1"/> </ColorAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#D8FFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#8CFFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#3FFFFFFF"/> </ColorAnimationUsingKeyFrames>--> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Disabled"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="OuterBorder" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="00:00:00" Value="#66FFFFFF"/> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="InnerWhiteBorder" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="00:00:00" Value="#66AB5A44"/> </ColorAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="(UIElement.Opacity)"> <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.4"/> </DoubleAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="00:00:00" Value="#66FFFFFF"/> </ColorAnimationUsingKeyFrames> <!--<DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value=".55"/> </DoubleAnimationUsingKeyFrames>--> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="FocusStates"> <vsm:VisualState x:Name="Focused"> <Storyboard> <!--<DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity"> <SplineDoubleKeyFrame KeyTime="0" Value="1"/> </DoubleAnimationUsingKeyFrames>--> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Unfocused"/> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Path x:Name="OuterBorder" Stretch="Fill" Data="M0,0 L0,16.119268 L3.8264601,20.001049 L32.971329,19.935255 L36.797791,15.461338 L36.670242,-0.19737187" Margin="0.481,0.498,-0.544,-0.686" > <Path.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF90645B"/> <GradientStop Color="#FF5B2014" Offset="1"/> </LinearGradientBrush> </Path.Stroke> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF5D2317" Offset="0"/> <GradientStop Color="#FF5D2317" Offset="1"/> </LinearGradientBrush> </Path.Fill> </Path> <Path x:Name="InnerWhiteBorder" Height="19" VerticalAlignment="Top" Stretch="Fill" Data="M0,0 L-0.057142854,15.034721 L4.1142855,18.041666 L42.716953,17.999807 L46.641251,14.609146 L46.641247,-0.041859776" Margin="1.313,0.042,0.687,0" d:LayoutOverrides="VerticalAlignment"> <Path.Fill> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFCA877C"/> <GradientStop Color="#FFB05546" Offset="0.5"/> <GradientStop Color="#FF9E230D" Offset="0.5"/> <GradientStop Color="#FFAB5A44" Offset="1"/> </LinearGradientBrush> </Path.Fill> <Path.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFC3AAA8"/> <GradientStop Color="#FFB38A84" Offset="1"/> </LinearGradientBrush> </Path.Stroke> </Path> <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" RenderTransformOrigin="0.5,0.5"> <ContentPresenter.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform/> <TranslateTransform Y="-2"/> </TransformGroup> </ContentPresenter.RenderTransform> </ContentPresenter> <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" RadiusX="3" RadiusY="3" IsHitTestVisible="false" Opacity="0"/> <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" RadiusX="2" RadiusY="2" Margin="1" IsHitTestVisible="false" Opacity="0"/> </Grid> </ControlTemplate> </Button.Template> </Button> </UserControl>
There's no C# code behind made for this object.
05-23-2009 6:46 PM |
Added a background on the UserControl and even on the Button Grid in the template and style not working
05-23-2009 6:53 PM |
Ok I thing I found why. The Button which was templated, was put in a UserControl when I took it I put it in my library and referenced the library and put the control on my form with the event MouseLeftButtonDown
Now I took the button out of the UserControl and put the Tamplated Button directly on my form and now I have the Click event and it works.I still don't understand why it wasn't working
Of course doing so, I have the xmal code of this button twice in my form.
05-24-2009 12:57 AM |
See below for the docs that seem relevant here...since you have a reskinned button that still functions like a regular button, i think this applies to your code and would explain why you didn't receive the raw mouse event and should instead work with the Click event.
Controls that inherit MouseLeftButtonDown can provide special handling for the event, by overriding the OnMouseLeftButtonDown method. This might include marking the Handled value of the event as true, which has the effect of suppressing the MouseLeftButtonDown event on any instance of the control (and potentially any subclass of the control). For instance, ButtonBase implements OnMouseLeftButtonDown in such a way that the mouse left button captures the mouse and raises the ButtonBase-defined Click event. The implementation also sets Handled, which prevents mouse button actions from being reported as a MouseLeftButtonDown event that could be handled by a button instance. This is done because the Click event is more meaningful for the control's intended purpose. For more information, see OnMouseLeftButtonDown"
05-24-2009 1:02 AM |
GearWorld:put in a UserControl when I took it I put it in my library and referenced the library and put the control on my form with the event MouseLeftButtonDown
This could just be due to the fact that the Button suppresses the MouseLeftButtonDown event. Are you sure the Click event wasn't firing in both cases (both in the user control and directly on the form)?
05-24-2009 9:04 PM |
The Click event wasn't abvailable in the UserControlThat's why I used the MouseLeftButtonDown
Since the button was in the UserControl My first step was to add this UC on my form and doing so I had accessonly to MouseLeftButtonDown since the Click event wasn't visible
05-25-2009 12:50 AM |
Ah...sorry it took me a while to understand what you were doing...I think I've got it now!
A few preliminary points:
A user control is a class. When you put the button inside the user control (and gave the button a name in the XAML), it became a field with 'internal' access level in the code behind. If you were writing code in your page (i.e. in the parent of the user control) you would have to reach in a couple of levels to get to the click event: first get the user control (assuming you named it in the XAML), then access the internal field that is the button (assuming you are in the same assembly), then you should see the Click event. In other words, since the Click event was only on the Button, and the Button was nested inside the user control, Click wouldn't be available directly on the user control itself (as you found out). Subcribing to the MouseLeftButtonDown on the user control didn't help because that event is on the user control itself, not on the Button nested inside the user control.
I'm not sure if creating a User Control is the right thing for you to do or not. Typically, custom User Controls are used for composing functionality from several pieces. A classic example is creating a "LoginControl" from 2 text blocks, 2 text boxes (for user name and password), and a button. In your case, since you were looking at the vista close button, you might want to make a User Control that contained three vista-style buttons: close, minimize, maximize. If you are trying to do something like that, then a User Control seems like the right strategy.
With a custom User Control, it's not common to expose the events of the contained objects. For example, the LoginControl would not expose the button Click event. Instead, it would handle the Click event internally and then raise its own event, maybe something like "UserLoggedIn". The page that contained the custom LoginControl would subscribe to the UserLoggedIn event and not worry about whether there was a button Click event going on behind the sceens inside the control.
05-25-2009 6:59 PM |
I see now that when you take something from others there's not necessarely everything with it.Yeah making a UserControl, I would have added my own event that is raised against the Click event of the button inside the UC
Well let consider this as solved. (FOR ME)