I am in the process of building a metro style app and I need to know if there is a way to fire button clicks programmatically.
I have this PasswordBox and button in my xaml:
<PasswordBox IsPasswordRevealButtonEnabled="True" KeyDown="On_text_KeyDown" x:Name="SomePW" FontSize="50" KeyDown="On_text_KeyDown" Margin="0,0,0,190" Height="67" Width="363"/>
<Button Background="White" x:Name="Button_Go" Foreground="Black" Margin="20,0,0,190" Content="Go" FontSize="20" Click="Go_click" Height="67" Width="60"/>
And in my C# code this is the function that handles the key press in the PasswordBox:
private void On_text_KeyDown(object sender, RoutedEventArgs e)
{
KeyEventArgs K = (KeyEventArgs)e;
if (K.Key == Windows.System.VirtualKey.Enter)
{
//<TO-DO> Simulate Button Click Here
}
}
The problem is I can't seem to find a way to simulate the button click... Can someone help please?
Is there a problem with simply calling this or are you looking for a generic way to invoke a Click event on any button, perhaps to automate it?
Go_click(this, new RoutedEventArgs());
You can try this:
ButtonAutomationPeer peer = new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv = peer.GetPattern( PatternInterface.Invoke ) as InvokeProvider;
invokeProv.Invoke();
Another option is the following:
SomeButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Hope this helps!!
Related
I created a UserControl, and added a Button inside it removing the Background and Text properties:
<Button x:Name="Button"
HorizontalAlignment="Center"
Height="40"
VerticalAlignment="Center"
Width="40"
RenderTransformOrigin="0,-2"
Margin="0,0,0,0"
BorderBrush="{x:Null}"
Click="Button_Click"
Background="{x:Null}"/>
I also hadled the Button Click event as below:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button.Content = new cross();
}
The above code fills the Button content with another UserControl which is a simple cross pic.
I have placed the UserControl with the Button into a MainWindow app and after pressing Button, it starts blinking - background is fluently changing between two colours. Beside my functionality from code works good. I just don't know how to get rid of that blinking background.
Before click:
After click:
You could set Focusable="False" at your Button to achive this.
But you should read about the Focusableproperty in the MSDN to check if it's ok for you. I guess you can't focus the Buttonusing the tab key anymore. But maybe that's not a problem for you.
I have seen a lot of debate about when to use tooltip and when to use popup but I don't know which one is better for my case.
I have a button. When I click on it, the popup panel will appear and it has a lot of text and a small image (so it will be a quite big panel). The panel must stay there until I move my cursor OFF THE BUTTON (it must still close when the cursor is still on the panel but off the button).
<Button Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="mypopup">
<stuff>
</Popup>
private void clicked(object sender, RoutedEventArgs e) {
mypopopup.isopen = true;
}
private void mouseleaved(object sender, MouseEventArgs e) {
mypopup.isopen = false;
}
This is where I got to so far. The problem is that sometimes, the Popup appears on top of the button (which blocks the view of the button and so MouseLeave event kicks off, and Popup instantly disappears). I want the Popup to stay until i move the cursor away off the button.
So I did some google, and I think Tooltip may avoid this problem. But how to get Tooltip to appear on button click and not button hover?
Which one is better for me? Tooltip or Popup?
EDIT
I think I was not too clear with my question. I am asking which one i should use- Tooltip vs Popup based on MY SPECIFIC SITUATION (paragraph 2) and not in general. I think Popup is the right one to use but I have problems with using it (paragraph 3). so my question is can I solve this problem with Popup or should I use Tooltip better for this?
But how to get Tooltip to appear on button click and not button hover?
Handle the Click event for the Button and set the IsOpen property of the Popup to true:
private void Button_Click(object sender, RoutedEventArgs e)
{
popup1.IsOpen = true;
}
<Popup x:Name="popup1" StaysOpen="False">
<TextBlock>popup content...</TextBlock>
</Popup>
<Button Click="Button_Click" Content="op" />
Which one is better for me? Tooltip or Popup?
Popup is preferable whenever you want to customize the behaviour in any way.
Edit: If I understand your issue correctly, this should work:
<Button x:Name="button" Content="Button" Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="popup" PlacementTarget="{Binding ElementName=button}" StaysOpen="True" MouseLeave="mouseleaved">
<Border Background="Yellow">
<TextBlock>contents...</TextBlock>
</Border>
</Popup>
private void clicked(object sender, RoutedEventArgs e)
{
popup.IsOpen = true;
}
private void mouseleaved(object sender, MouseEventArgs e)
{
if (!button.IsMouseOver && !popup.IsMouseOver)
popup.IsOpen = false;
}
I develop chat application on Xamarin.Forms.
I need to don't hide keyboard when I press Send button and hide keyboard when I tap anywhere else.
I made it for iOS.
Can I make the same for android?
Found a very simple method to achieve this goal, you can simply place a Button on the top of an Entry like this:
<Grid VerticalOptions="End">
<Entry x:Name="MessageEntry" TextChanged="MessageEntry_TextChanged_1" />
<Button x:Name="SendButton" Text="Send" HorizontalOptions="End" Clicked="Button_Clicked" IsEnabled="False" />
</Grid>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
MessageEntry.Text = null;
}
private void MessageEntry_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (MessageEntry.Text != null)
SendButton.IsEnabled = true;
else
SendButton.IsEnabled = false;
}
Tested on Android 6.0 emulator, works fine to me, you can customize the Button to make it looks more beautiful in this view:
you can have in your PCL a scrollview element in which you add you entry element, when you tap on the entry element it will bring the keyboard and not hide it, it only hide it when tapped on a different portion of the screen. let me know if it help
I have a Control that contains a Popup. I am trying to close the Popup whenever someone clicks outside of the Control. This is the part of my code that sets up the problem:
AddHandler(Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseuttonEventHandler(HandleOutsideClick), true);
Now whenever I click in the Popup it causes PreviewMouseDownOutsideCapturedElementEvent to be raised. Am I misunderstanding this event? Is there something that I can do to have the Popup be considered a part of the Control so that it doesn't raise this event?
Does this work?
<Popup Name="Pop" LostFocus="ClosePop"/>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
Put the XAML code in your .xaml page and the C# code in the related .xaml.cs file.
Note: You may need to put the focus on the popup before this works, it may be done automatically; I haven't done this on popups, but I have done it on other objects.
Update: This works for me, clicking in the TextBox that says Test1 opens the Popup, and clicking in the TextBox labeled Test2 closes it:
<Grid Background="White">
<StackPanel>
<TextBox Foreground="Black" LostFocus="ClosePop" GotFocus="OpenPop" Height="50">Test1</TextBox>
<TextBox Foreground="Black" Height="50">Test2</TextBox>
</StackPanel>
<Popup Name="Pop" Height="50" Width="50">
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center">Pop!</TextBlock>
</Popup>
</Grid>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = false;
}
private void OpenPop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = true;
}
In my app I have a textbox and when the user presses the return button on the keyboard it should act has a event for the textbox. Similar to if a user pressed a submit button.
<TextBox Name="textBox1" Grid.Row="0" Text="Search or Type URL" Background="White" BorderBrush="{x:Null}" Margin="62,0,125,0" Foreground="#FF6E6E6E" AcceptsReturn="True" />
Above is the XAML for the textbox, and the AcceptsReturn is set to true.
Do I add a return event handler?
Thank you in advance :)
If you need any more details please comment and I will be happy to explain in further detail :)
What you can do is handle the KeyDown event like this:
private void SomeTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
this.Focus(); // dismiss the keyboard
// Call the submit method here
}
}
NOTE: AcceptsReturn means that when Return/Enter is pressed on the keyboard, it will add a newline/Return character, and not dismiss the keyboard.