Button color change on click or hover - xaml

I added a button component in my application, and I am expecting it to change the color when user hovers on it or click it.
But looks like that's not the default behavior of the button.
Is there a way to get that behavior maybe by setting some attribute?

You can simply use a event handler to set the color of the button when clicking it.
Here's the code sample below for your reference:
<Button Clicked="Button_Clicked">
Code in backend:
private void Button_Clicked(object sender, EventArgs e)
{
var button = (Button)sender;
button.BackgroundColor = Color.FromArgb("#FFA07A");
}
Update:
I found a simialr issue for your scenerio in Github, you can follow it up here:https://github.com/dotnet/maui/issues/9552

Related

I'm trying to go to another page when clicking a button. How could i do this (WinUI 3)

I'm trying to go to another page when clicking a button. How could i do this.
I tried using a NavigationViewItem but it didn't work.
The NavigationViewItem you posted is used in UWP.
If it is a WinUI3 project, then you need to refer to this document, use Navigate.
the relevant code samples are as follows.
xaml
<Button x:Name="myButton" Click="myButton_Click"/>
xaml.cs
private void myButton_Click(object sender, RoutedEventArgs e)
{
Frame rootFrame = new Frame();
this.Content = rootFrame;
rootFrame.Navigate(typeof(BlankPage1), null);
}

How to make a default button in UWP app using XAML?

I'm trying to declare a button as default in UWP app but receive an error:
The property 'IsDefault' was not found in type 'Button'
How can I make a default button in UWP app?
I down know what IsDefault is in WPF but to get if a button is pressed in UWP you can use CoreWindow.GetForCurrentThread().KeyDown. Create a Method that will be called from when the button is pressed or VirtualKey.Enter is clicked.
public MainPage()
{
this.InitializeComponent();
CoreWindow.GetForCurrentThread().KeyDown += MainPage_KeyDown; ;
}
private void MainPage_KeyDown(CoreWindow sender, KeyEventArgs args)
{
switch (args.VirtualKey)
{
case Windows.System.VirtualKey.Enter:
// handler for enter key
break;
default:
break;
}
}
You can use key down event which you can place on any textbox for example if you are making a login page then probably there will be 2 textboxes for username and password then just add key down event handler to textbox as it will be the last mandatory field like this:
<PasswordBox KeyDown="PasswordKeyDown"/>
then you can handle this event as:
using System.Windows.Input;
private void PasswordKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
Login();
}
Hope it will help you :)
There is not easy or clean way to solve your problem because IsDefault is not available for uwp apps.
if you are using MVVM or you want to reuse your code I recommend you to use Behaviors and follow the examples that the other guys posted.
I need a Button which user can invoke by pressing the ENTER key.
In an UWP app, by default a Button can be invoked by pressing the Enter key. So I guess what you want is setting the focus on this Button when there are some other UIElements in your page.
You can refer to Keyboard navigation among UI elements,
By default, the tab order of controls is the same as the order in which they are added to a design surface, listed in XAML, or programmatically added to a container.
To focus on the Button which is not the first element, you can just give the TabIndex="1" property to your Button, this property can make your Button get focus whenever the page is loaded, but if you change the focus on other controls in this page, you will need to reselect this button by mouse clicking, touching or TAB key.

Hide virtual keyboard in windows 8 metro

Now i am working on Windows 8 Metro application. For that i have a popup window with textbox and OK button. I need to hide Virtual Keyboard when "Enter" is clicked on Virtual keyboard. If i click "OK" button on popup keyboard hides automatically.
I got this Link as good reference (using HiddenField). Is there any way to done this work without using "HiddenField". Thanks in advance..
Well finally found a solution for this issue.. I just change the focus from textbox to button in my popup.. below is the sample code..
public void FocusTextbox(object sender, EventArgs e)
{
// set focus to textbox on popup open
Textbox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
public void Textbox_KeyDown(object sender, KeyRoutedEventArgs e)
{
// conforming the "Enter" button click
if (e.Key == Windows.System.VirtualKey.Enter)
{
// change the focus to OK button
this.OkButton.Focus(Windows.UI.Xaml.FocusState.Pointer);
}
}
Change the focus before closing the popup... it works great now..
And changing the focus to Label or Textblock is not hiding the Virtual keyboard...
i was also looking for this , but i found this approach first

Close popup modal dialog on clicking the submit button in sharepoint custom form

I have a custom form and I would like to add some functionality in my submit button. When I click on Submit button, I would like to close the popup and refresh the parent page.
I used this code in my button but there is a problem. It does not validate the controls and it makes a postback and even does not save any data.
OnClientClick="javascript:window.frameElement.commitPopup();"
Can you help me with some working code?
Thank you.
You need to set OnClientClick property after the code you want to be executed.
protected void btnAdd_Click(object sender, EventArgs e)
{
//Your Code here(some functionality)
Context.Response.Write(#"<script type='text/javascript'>window.frameElement.commitPopup(); return false</script>");
Context.Response.Flush();
Context.Response.End();
}

Forcing Default button on a gridview

I'm using gridview with templates to show and edit some information from a sql database.
When I edit and change the data in that row and then click enter it automatically presses the highest on page button which uses submit to server set to true which means it'll try to delete instead of update.
I've have tried setting a panel round the gridview and setting the panel's default button to the "updatebutton" but it won't allow that because it can't 'see' the buttons.
I had a similar problem and I found a very simple workaround:
Place a button below the GridView and make it invisible by CSS (i.e. position: relative; left: -2000px)
Create a Panel around the GridView and give it as DefaultButton the ID of the button we just created.
Write the following line of code for the click-event of the button:
myGridView.UpdateRow(myGridView.EditIndex, false);
Whenever you press enter in the GridView now, the edited row will be confirmed.
You need to precess KeyDown or KeyPress event of the grid, and check if pressed key if Keys.Enter :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, EventArgs.Empty);
}
}
private void button1_Click(object sender, EventArgs e)
{
// Your logic here
}
}