How to click a Button inside of a Button? C# WPF - c#

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)

Related

Programmaticaly click a button with it's UserControls

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)
{
}

Keyboard accelerator firing when writing to Textbox

Click event of Button is fired when pressing D while writing to Textbox.
Is there an elegant way how to suppress Keyboard Accelerator while Textbox is focused?
XAML:
<StackPanel Orientation="Vertical">
<TextBox></TextBox>
<Button Click="Button_Click" Content="Button with "D" as keyboard accelerator" Margin="0,10">
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
</Button>
<TextBlock x:Name="ButtonClickCounter"></TextBlock>
</StackPanel>
C#:
int buttonClickCounter;
private void Button_Click(object sender, RoutedEventArgs e)
{
ButtonClickCounter.Text = $"Button clicked {++buttonClickCounter} times";
}
EDIT:
Why Accelerator with Modifier (Alt+D or Ctrl+D) is not solution?
I am creating video player and I found that one-key shortcuts are neat solution for fast operations with video player (same as in VLC).
Best Solution so far:
Creating custom KeyboardAccelerator, that checks if focus is set to text box. Only edit in code that need to be done is changing KeyboardAccelerator to AcceleratorWithHandledActionIfTextboxIsFocused.
public class AcceleratorWithHandleDActionIfTextboxIsFocused:KeyboardAccelerator
{
public AcceleratorWithHandleActionIfTextboxIsFocused()
{
Invoked += AcceleratorWithHandleActionIfTextboxIsFocused_Invoked;
}
private void AcceleratorWithHandleActionIfTextboxIsFocused_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
var focusedElement = FocusManager.GetFocusedElement();
if (focusedElement.GetType() == typeof(TextBox))
args.Handled = true;
}
}
part of XAML:
<Button.KeyboardAccelerators>
<custom:AcceleratorWithHandleDActionIfTextboxIsFocused Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
I agree with #Thomas Weller in his comments as for not using a single letter as a keyboard accelerator...
But everybody has their own requirements, anyway, you could try to e.handle = true your event when textbox is focused.
Something like this:
public void Button_Click(object sender, ExecutedRoutedEventArgs e)
{
if (textBox.isFocused)
{
e.handled = true;
}
}

Why MouseDown event handler is not getting hit?

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.

How to detect DoubleClick in WPF ToggleButton

In my WPF application I have Toggle Button, I want to detect when user double click on it (in both cases if it checked or unchecked).
How can I do that?
Thanks in advance
You can use the OnPreviewMouseDoubleClick event
xaml:
<ToggleButton Height="75" Width="100" PreviewMouseDoubleClick="Control_OnPreviewMouseDoubleClick"/>
code-behind:
private void Control_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var toggleButtton = sender as ToggleButton;
if (toggleButtton != null)
{
if (toggleButtton.IsChecked.HasValue)
{
if (toggleButtton.IsChecked.Value)
{
// Checked
}
else
{
// Unchecked
// this will re-check the button if double-click unchecks it
//
toggleButtton.IsChecked = true;
toggleButtton.Focus();
}
}
}
}
Use PreviewMouseDoubleClick event (msdn):
XAML:
<ToggleButton x:Name="tButton" Height="30" Content="MyButton"
PreviewMouseDoubleClick="tButton_PreviewMouseDoubleClick"
/>
Code-behind:
private void tButton_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
tButton.IsChecked = !tButton.IsChecked.Value;
e.Handled = true;
//...
}
This event is pretty easy with togglebuttons
Xaml you write the following to get an EventHandler:
<ToggleButton Name="button1" MouseDoubleClick="button1_DoubleClick" />
In c# you write the following to get an EventHandler:
button1.MouseDoubleClick += new MouseButtonEventHandler(button1_DoubleClick);
And in both cases you need:
void button1_DoubleClick(object sender, MouseButtonEventArgs e)
{
}

Action on both button press and release

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

Categories