I have this very simple button in a WPF that is supposed to call a function preview() on MouseDown, and function hide() on MouseUp. However it's not working and not even hitting its event handler. What is my mistake?
I tried changing the background property, no use. MouseLeave and MouseClick both work but that's not what I want.
XAML:
<Button x:Name="previewButton" Background="#FF383434" Margin="5,5,8,0" MouseDown="previewButton_MouseDown" MouseUp="previewButton_MouseUp" MouseLeave="previewButton_MouseLeave" TouchDown="previewButton_TouchDown" TouchUp="previewButton_TouchUp" TouchLeave="previewButton_TouchLeave" Grid.Row="1" Click="previewButton_Click" Padding="0" >
<StackPanel Height="98" Width="49">
<Image Source="/EZ3D;component/Resources/old/eye.png" Margin="0,17,0,0" />
<TextBlock Text="Preview" TextWrapping="WrapWithOverflow" Margin="0" HorizontalAlignment="Center" Padding="0" FontSize="13" VerticalAlignment="Center"/>
</StackPanel>
</Button>
Code Behind
private void previewButton_TouchDown(object sender, TouchEventArgs e)
{
ShowPreviewImage();
}
private void previewButton_TouchUp(object sender, TouchEventArgs e)
{
HidePreviewImage();
}
private void previewButton_TouchLeave(object sender, TouchEventArgs e)
{
HidePreviewImage();
}
private void previewButton_MouseDown(object sender, MouseButtonEventArgs e)
{
ShowPreviewImage();
}
private void previewButton_Click(object sender, RoutedEventArgs e)
{
ShowPreviewImage();
}
private void previewButton_MouseUp(object sender, MouseButtonEventArgs e)
{
HidePreviewImage();
}
private void previewButton_MouseLeave(object sender, MouseEventArgs e)
{
HidePreviewImage();
}
Handle PreviewMouseDown instead of MouseDown event, PreviewMouseUp instead of MouseUp and so on. It should work for you.
<Button x:Name="previewButton" Background="#FF383434" Margin="5,5,8,0" PreviewMouseDown="previewButton_MouseDown" MouseDown="previewButton_MouseDown" PreviewMouseUp="" MouseUp="previewButton_MouseUp" MouseLeave="previewButton_MouseLeave" TouchDown="previewButton_TouchDown" TouchUp="previewButton_TouchUp" TouchLeave="previewButton_TouchLeave" Grid.Row="1" Click="previewButton_Click" Padding="0" >
<StackPanel Height="98" Width="49">
<Image x:Name="Image1" Source="/EZ3D;component/Resources/old/eye.png" Margin="0,17,0,0" />
<TextBlock Text="Preview" TextWrapping="WrapWithOverflow" Margin="0" HorizontalAlignment="Center" Padding="0" FontSize="13" VerticalAlignment="Center"/>
</StackPanel>
</Button>
All FrameworkElements expose these events. All 'Preview...' events are 'Tunnel' events while the other Mouse events are 'Bubble' events. The tunnel events are raised first on higher level elements, e.g. if you mouse over a button element, the first Preview mouse event goes to the Window and then on down through all of its descendants until it reached the ultimate target, in this case the button. Then the normal, i.e. non-preview' mouse event starts bubbling up from there until it reaches the Window. Anywhere along this chain an event handler can mark the event as handled and stop the process. Here the MouseDown bubbling event gets handled by your textblock placed inside the button.
Certain controls handle input events internally, you usually can use the tunneling version of events (Preview*) in those cases. See MSDN.
Related
Like you can see here, i got a button with Name, etc. and i got in this button a usercontrol. When i click on the button everything is alright, but when i programmaticaly click this button. The UserControls will not be performed... why?
<Button x:Name="btnHome" Style="{DynamicResource PopupButtonStyle}" MouseEnter="btnHome_MouseEnter" MouseLeave="btnHome_MouseLeave" Click="btnHome_Click">
<ctls:MenuItem GroupName="MenuItem" IndicatorBrush="{DynamicResource PrimaryBlueColor}" Icon="{DynamicResource home}" IconWidth="16" Text="Home" VerticalAlignment="Center"/>
</Button>
This is the code for clicking the button, am i wrong here?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
btnHome.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void btnHome_Click(object sender, RoutedEventArgs e)
{
}
I created some buttons with another button inside. Then I want to click the inner button but it only throws me the outer button click event, not even both. I tried the solutions in this question but didn't achieve anything. The exact goal that I have is to only load the click method of the inner button when i click it, and if I click in wherever else in the outer button it throws me the respective method.
//Outer button click event
void newBtn_Click(object sender, EventArgs e) {
//Stuff
}
newBtn.Click += new RoutedEventHandler(newBtn_Click);
//Inner button click event
void editButton_Click(object sender, RoutedEventArgs e)
{
//Stuff
editButton.Click += new RoutedEventHandler(editButton_Click);
e.Handled = true;
}
//stuff
It would be better to share your code asking a question.
By the way this sample should work:
<Button Background="Red" Width="100" Height="50" Click="Button_Click">
<Button Background="Green" Margin="10" Width="50" Height="50" Click="Button_Click_1" />
</Button>
In the event handler of the inner button simply add this, as suggested by the post you mentioned:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
This will stop the chain of Routed Events (Bubbling and Tunneling)
I have to play a video using MediaElement. I want to pause the video when the user taps the screen. I found that there is double tapped event on MediaElement, but couldn't find any single touch event. How can I do this?
<MediaElement Name="videoMediaElement" AreTransportControlsEnabled="True" Stretch="Fill"
MediaOpened="videoMediaElement_MediaOpened" CurrentStateChanged="Media_State_Changed">
<MediaElement.TransportControls>
<MediaTransportControls Background="Red" Foreground="White"
IsStopButtonVisible="True" IsStopEnabled="True" IsTextScaleFactorEnabled="True"
IsPlaybackRateEnabled="True" IsPlaybackRateButtonVisible="True"
IsFastForwardButtonVisible="True" IsFastForwardEnabled="True"
IsFastRewindButtonVisible="True" IsFastRewindEnabled="True"/>
</MediaElement.TransportControls>
</MediaElement>
private async void Media_State_Changed(object sender, RoutedEventArgs args)
{
if (videoMediaElement.CurrentState == MediaElementState.Paused)
{
}
}
There are plenty of events you can use for that, e.g. the MouseLeftButtonDown or TouchDown events. It's as simple as that:
private void element_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
((MediaElement)sender).Pause();
}
private void element_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
{
((MediaElement)sender).Pause();
}
By #JetChopper:
private void element_PointerPressed(object sender, PointerRoutedEventArgs e)
{
((MediaElement)sender).Pause();
}
it's a Rectangle inside a Grid in my program
<Grid x:Name="mainGrid" Background="Transparent" MouseLeftButtonDown="mainGrid_MouseLeftButtonDown">
<Rectangle x:Name="rectangle" MouseEnter="rectangle_MouseEnter" Focusable="True" MouseLeftButtonDown="rectangle_MouseLeftButtonDown" MouseLeave="rectangle_MouseLeave" Fill="#FFF4F4F5" Margin="2" Stroke="Transparent"/>
</Grid>
and mouse left button down events:
private void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("mouse left button down on rectangle");
}
private void mainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("mouse left button down on mainGrid");
}
it's output when mouse button downs on rectangle:
mouse left button down on rectangle
mouse left button down on mainGrid
I want just rectangle mouse event rise up when click in rectangle, and grid mouse event rise up when click outside the rectangle
how can I do that?
MouseLeftButtonDown is bubbling event which means it will go up the visual tree from originating element. You can limit the execution of the handler by setting Handled to true in rectangle_MouseLeftButtonDown
private void rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Debug.WriteLine("mouse left button down on rectangle");
}
I can't get this straight: I have one button that I want one action for the press event and one action for release, I've searched everywhere and can't find an answer.
KeyDown, KeyUp or MouseLeftButtonDown doesn't work with button on windows phone 7.
First I tried combining GotFocus and Click clickmode release like this:
(As you can see I want Image1 to be shown while pressing button, and hidden when releasing the button)
xaml:
Button Click="button1_Click" ClickMode="Release" GotFocus="button1_GotFocus" Content="byt" Height="72" Margin="0,500,6,0" Name="button1" VerticalAlignment="Top"
private void button1_GotFocus(object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
}
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Visible;
}
This works only one time and could work all the time if I could loose focus from the button when releasing it (tried searching for that as well)
The other thing I tried was changing the clickmode while pressing the button, but didn't get that to work either..
something like this:
private void button1_Click (object sender, RoutedEventArgs e)
{
Image1.Visibility = System.Windows.Visibility.Collapsed;
button1.SetValue(Button.ClickModeProperty, ClickMode.Release);
Image1.Visibility = System.Windows.Visibility.Visible;
}
(I know that the syntax is wrong somehow in the second one)
Would be grateful for help!
MouseLeftButtonDown / MouseLeftButtonUp do work on WP7. Obviously not named the best, but they do work on the device.
<TextBlock x:Name="ApplicationTitle" MouseLeftButtonDown="ApplicationTitle_MouseLeftButtonDown" MouseLeftButtonUp="ApplicationTitle_MouseLeftButtonUp"
Style="{StaticResource PhoneTextNormalStyle}"
Text="MY APPLICATION" />
You'll see Down gets fired, and then immediately Up.
private void ApplicationTitle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
private void ApplicationTitle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
Perhaps all you need is MouseLeftButtonDown/Up.
I used
Click="{x:Bind UpArrow_Click}"
ClickMode="Press"
PointerCaptureLost="Button_Release"
on a button. When button is pressed UpArrow_Click event occurs... when button is released Button_Release event occurs