Scrolling up the screen when Keyboard appears - xaml

Is there a way to scroll-up view the required amount when the soft-keyboard shows up? The page in my app has a lot of textboxes and when the keyboard shows up, a lot of them hide under it and the user has to manually scroll down/hide keyboard to enter values in the other textboxes.
How can I scroll-up the page by some amount to improve UX?

You can have a KeyDown event set for each textbox and inside event handler check if Enter key is pressed shift focus to next textbox.
KeyDown="txtMessage1_KeyDown"
private void txtMessage1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
nextTextbox.Focus();
}
}

Related

How to close ComboBox list items when moving application window of my WinRT/C++ UWP application?

I have a pair of ComboBox controls having IsEditable() true as well as false.
When I am scrolling through my application or moving my application window (by clicking on the title bar) with list popup open, I would like to close the ComboBox list popup as otherwise there would be a weird delay in aligning the list correctly below the control.
Is this possible in UWP with WinRT/C++? If so, kindly suggest how to.
I did an investigation to find if any events are there to handle in such a scenario when ComboBox control is essentially displaced from initial position while moving the app window/scrolling the app, but couldn't find any help.
Edit: Adding ComboBox image from XAML Controls Gallery to demonstrate the behaviour. In case if IsEditable set as true, when popup is opened and application is scrolled then popup goes outside the window. Instead I would like to dismiss the popup itself. However, if IsEditable is set as false then we cannot scroll until the popup is dismissed.
Update: The code I tested for PointerWheelChanged
void CBFile2022X::OnPointerWheelChangedHandler( Windows::Foundation::IInspectable const& sender,
Windows::UI::Xaml::Input::PointerRoutedEventArgs const& eventargs )
{
OutputDebugString( L"PointerWheelChanged" );
if( ComboBox != nullptr )
{
ComboBox.IsEnabled( false );
ComboBox.IsEnabled( true );
}
}
I have to say that currently there is no event to detect if the application window is moved or changed its location.
Update:
You could handle the UIElement.PointerWheelChanged Event which will be fired when users scroll the mouse wheel. You could set the IsEnabled property of the ComboBox to false first and then set it to true, this will make the ComboBox lose its focus. Like:
private void Mypanel_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
FontsCombo.IsEnabled = false;
FontsCombo.IsEnabled = true;
}
Update2:
If you are using a ScrollViewer you could try to handle the ScrollViewer.ViewChanging Event.
private void ScrollViewer_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
{
FontsCombo.IsEnabled = false;
FontsCombo.IsEnabled = true;
}

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

How to force LostFocus when button is clicked

XAML C# not WEB page is a Window
At the click of a button I:
Need to trap the name of the last control OnFocus
Force the LostFocus event of the control.
// PROBLEM: clicking on btns does not force lostfocus event on the last entered element control (last entry control could be text,checkbox or others)
added save button, where calling such method it moves focus to parent forcing lostfocus on last element.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
AcceptLastFocusedElement(sender, e);
}
private void AcceptLastFocusedElement(object sender, RoutedEventArgs e)
{
FocusManager.SetFocusedElement(this, (Button)sender);
}
NOTE: no need for task number 1 (getting the name of the element).
You could make use of the LayoutUpdated method.
So whenever there is any event happening, it goes into the LayoutUpdatedevent and you could trap the LastFocusObject .

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