I have a button name mybutton that is a part of a usercontrol X.
I made another usercontrol Y that holds X (X is part of it).
I want to catch in Y the event of mybutton when clicked.
How can I do it ?
Thanks
Look at the following code. It's the same with UserControl instead of Grid. The Click event is routed up the visual tree, read more here.
<Grid Button.Click="Button_Clicked">
<Grid>
<Button Content="Save" />
</Grid>
</Grid>
private void Button_Clicked(object sender, RoutedEventArgs e)
{
//your code here
}
Related
So i have the TextBox:
<TextBox Controls:TextBoxHelper.ClearTextButton="True"
LostFocus="textboxNewValueCell_LostFocus"
TextChanged="textboxNewValueCell_TextChanged"/>
And when press on Clear button i want to catch the event.
Is it possible ?
I did not find any event
The ClearTextButton simply calls Clear() on the TextBox. There is no specific event raised. The best you can do is to handle the TextChanged event:
private void textboxNewValueCell_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Length == 0)
{
//the TextBox was cleared and the Button was maybe clicked...
}
}
First, give your TextBox a name. Then, create a click event on the Button. when the click event fires, handle the clearing of the TextBox in the CodeBehind.
<Grid>
<TextBox x:Name="MyTextBox" Text="Some Text"/>
<Button x:Name="ClearButton" Click="ClearButton_Click"/>
</Grid>
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
MyTextBox.Text = string.Empty;
}
I have created a UserControl in WPF which consists of 2 simple buttons in the first run.
Now, I want to display in a MessageBox the x:Name of the button which is clicked by the user, but I don't want to create a Clicked event for each button separately.
Is it possible to program 1 generic Clicked event in the UserControl and then identify the sender object to get the correct x:Name ?
Is it possible to program 1 generic Clicked event in the UserControl and then identify the sender object to get the correct x:Name ?
Sure:
<Button x:Name="first" Click="generic_Click" />
<Button x:Name="second" Click="generic_Click" />
private void generic_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
MessageBox.Show(clickedButton.Name);
}
Use an EventSetter for that in a style for a button.
Example in xaml:
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.EventOvw2"
Name="dpanel2"
Initialized="PrimeHandledToo"
>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<EventSetter Event="Click" Handler="b1SetColor"/>
</Style>
</StackPanel.Resources>
<Button>Click me</Button>
<Button Name="ThisButton" Click="HandleThis">
Raise event, handle it, use handled=true handler to get it anyway.
</Button>
</StackPanel>
And then in cs file:
void b1SetColor(object sender, RoutedEventArgs e)
{
Button b = e.Source as Button;
b.Background = new SolidColorBrush(Colors.Azure);
}
void HandleThis(object sender, RoutedEventArgs e)
{
e.Handled=true;
}
Is it possible to click Button which is covered by Border?
If it is, how to handle occurred events?
This is page layout:
<Page x:Class="App6.MainPage"
<!-- ... -->
>
<Grid>
<Button Content="click" />
<Border Background="Transparent" />
</Grid>
</Page>
The main idea is to catch gesture manipulations (with Border) but also allow using controls.
Thanks in advance for any help.
Thanks to this I found the way to solve this issue.
Just need to add handler and set parameter handledEventsToo in true:
public MainPage()
{
this.InitializeComponent();
this.Button.AddHandler(PointerPressedEvent, new PointerEventHandler(pointerPressedEvent), true);
}
private void pointerPressedEvent(object sender, PointerRoutedEventArgs e)
{
}
After that you can see UI callback after clicking the button
Does anybody know how to implement a double-click event handler that opens a new window in a way the new window becomes the front most window? (Just the behavior that is normally expected).
In WPF there is a strange behavior of windows when opening a second window in the double-click event handler. The second window opens but the first window, where the double-click-event was fired, becomes activated again immediately.
Opening a window in a click event handler, works as expected. The second window opens and remains the front window.
For demonstration purposes I created the following application. Two window classes with just a button control. To distinguish between click and double-click on the button control, the click-event works only if the left shift key is pressed.
After double-click
http://blog.mutter.ch/wp-content/uploads/2014/05/wpf_window1.png
After click (this is also the expected behavior for double-click)
http://blog.mutter.ch/wp-content/uploads/2014/05/wpf_window2.png
Main Window
<Window x:Class="WpfWindowSwitching.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="600">
<Grid>
<Button Margin="40"
HorizontalAlignment="Center"
VerticalAlignment="Center"
MouseDoubleClick="doubleClick"
Click="click">
<TextBlock FontWeight="Bold"
FontSize="22">
I am the first Window, double click this button...
</TextBlock>
</Button>
</Grid>
</Window>
The code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void doubleClick(object sender, MouseButtonEventArgs e)
{
openNewWindow();
}
private static void openNewWindow()
{
var window = new SecondWindow();
window.Show();
}
private void click(object sender, RoutedEventArgs e)
{
if (!Keyboard.IsKeyDown(Key.LeftShift)) return;
openNewWindow();
}
}
Second Window
<Window x:Class="WpfWindowSwitching.SecondWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SecondWindow" Height="200" Width="600">
<Grid>
<Button Margin="40"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="click">
<TextBlock FontWeight="Bold"
FontSize="22">
I am the second Window
</TextBlock>
</Button>
</Grid>
</Window>
The code behind:
public partial class SecondWindow : Window
{
public SecondWindow()
{
InitializeComponent();
}
private void click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
After MouseDoubleClick event, MouseUp event is raised which gets handled on MainWindow. Hence secondary window gets activated momentarily and with subsequent event bubbling, main window gets activated.
In case you don't want that, you can explicitly stop event bubbling by setting e.Handled to True after mouse double click event. This way secondary window will remain activated.
private void doubleClick(object sender, MouseButtonEventArgs e)
{
openNewWindow();
e.Handled = true;
}
I have two controls stacked on top of each other as such:
Button1 is ZIndex 2, Button2 is ZIndex 1.
Both need to be hit testable, as I need to listen to Button1's MouseEnter event for other system functions. But I also need to be able to have Button2 be clickable. I'm not quite sure how I can get this functionality to pan out (since I can't just set the Button1 to IsHitTestableFalse). Is there some sort of way I can say, in Button1's Click event
if (hasElementBeneath):
click that element instead
If I understand you correctly the trick is rather to prevent the "click" from the inner button to be handled by the outer button too.
To accomplish this: In the click handler for the inner button set the Handled property of the supplied RoutedEventArgs to true.
Like this:
<Button Click="Button1_Click" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel>
<TextBlock>Button 1</TextBlock>
<Button Click="Button2_Click" Margin="10">Button 2</Button>
</StackPanel>
</Button>
and:
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button1 clicked!");
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button2 clicked!");
e.Handled = true;
}