Windows Phone: Move focus to text box when keyboard enter is pressed/ - xaml

I have a windows phone page with the following UI markup:
<StackPanel Orientation="Vertical">
<TextBox Name="txtName"/>
<PasswordBox Name="txtPassword"/>
<Button Name="btnLogin" Content="Login"/>
<StackPanel>
What I want to do: After the user types the user name and clicks the "Enter" key on the keyboard, I want the focus to move to the next "txtPassword" TextBox , then when presses "Enter" the focus moves to the button and so on...
How can I achieve this ?

Xaml:
<StackPanel Orientation="Vertical">
<TextBox x:Name="txtName" KeyDown="TxtName_KeyDown" />
<PasswordBox x:Name="txtPassword" KeyDown="TxtPassword_KeyDown"/>
<Button x:Name="btnLogin" Content="Login" />
</StackPanel>
Cs:
private void TxtName_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
txtPassword.Focus();
}
private void TxtPassword_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
btnLogin.Focus();
}
Useful info: Determining the Enter key is pressed in a TextBox

Related

UWP: How to fix focus on particular UI element so that i can receive key press events?

So i have chat window with a text box, a mic button and some other buttons for settings and sharing. So i want to always switch on the mic whenever space button is pressed, except when the textbox has focus. No matter if other buttons are clicked last, space-bar should turn-on the mic.
Any help or pointers?
Here is a working sample.
Code Behind
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if (args.VirtualKey == Windows.System.VirtualKey.Space && txtData.FocusState == FocusState.Unfocused))
{
txtData.Text = "Mike Called";
}
}
}
txtData here is my TextBox that I an checking if I have FocusState Unfocused
XAML
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBox Text="Hello World" Name="txtData" Width="300" Height="35" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Page>
You should subscribe the KeyDown event of Window.Current.CoreWindow. It will be fired everytime user press any key, regardless of focused control. Only your app has to be focused.
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
if (args.Handled)
{
return;
}
// Your event handling
}

Long list selector windows phone child control event in DataTemplate

I am searching for this long time and i couldn't get it.
I have a Long list selector in my windows phone 8 project.
How can i manage the button event in each item in the data template? I need to get the selected item in that button event.
Code snippet shown below. Please help.
try this
// in your button click event type this code
var selectedValue = ((sender as Button).dataTemplate;
or
var selectedValue = ((sender as Button).dataTemplate as SbCaDd).AcNo;
If you want to access the dataContext then try this one.
XAML
<phone:LongListSelector Grid.Row="1"
Name="llsMsg"
LayoutMode="List"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Text="{Binding}"
Foreground="Black" />
<Button Content="View Details"
Width="200"
Click="Button_Click"/>
</Grid>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
var dataContext = (sender as Button).DataContext;
var dataContext = (sender as Button).DataContext as YourDataModel;
}

WP 8.1 ToggleButton Change Icon when Checked / UnChecked

I have the following XAML code for Windows Phone 8.1 (non SilverLight):
<Grid>
<ToggleButton Name="TogBtn" VerticalAlignment="Center" HorizontalAlignment="Center" Checked="ToggleButton_OnChecked">
<SymbolIcon Symbol="play"></SymbolIcon>
</ToggleButton>
</Grid>
The output of the above code is:
How can I change the icon to a stop icon when the toggle button is checked and then back to play icon when unchecked?
I thought this would be easy to find through Google, but apparently not.
Please change your XAML to this:
<Grid>
<ToggleButton x:Name="TogBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked">
<SymbolIcon Symbol="Play"></SymbolIcon>
</ToggleButton>
</Grid>
And please add this to your .cs file:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Stop);
}
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
TogBtn.Content = new SymbolIcon(Symbol.Play);
}
That should do the job!

watermarked PasswordBox in winrt

is it possible to get a watermarks passwordbox in WinRt? It is no problem to get a textbox with a watermark, but I don't know a toolkit where I can get a password box with a watermark.
How can I implement one for myself?
Take a look on WinRT XAML Toolkit.
They also have
WatermarkTextBox
WatermarkPasswordBox
By yourself you can implement your own controls:
in .xaml:
<Border x:Name="brdPassword" Margin="5,0,5,10" BorderThickness="2" BorderBrush="White" CornerRadius="5" Grid.Row="0"
Background="White" Height="50" VerticalAlignment="Stretch">
<Grid>
<TextBox x:Name="PasswordWatermark" TextWrapping="Wrap"
Text="Watermark" Foreground="#FFC4C4C4" IsHitTestVisible="False"
Background="{x:Null}" BorderThickness="0" Padding="0,-10"
FontSize="26.667" />
<PasswordBox x:Name="pbPassword" LostFocus="PasswordLostFocus"
GotFocus="PasswordGotFocus" Background="{x:Null}"
FontSize="26.667" Margin="0,-12,0,-9" VerticalAlignment="Center"
BorderThickness="0" Opacity="0" />
</Grid>
</Border>
in .cs
private void PasswordLostFocus(object sender, RoutedEventArgs e)
{
CheckPasswordWatermark();
}
private void CheckPasswordWatermark()
{
var passwordEmpty = string.IsNullOrEmpty(pbPassword.Password);
PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
pbPassword.Opacity = passwordEmpty ? 0 : 100;
}
private void PasswordGotFocus(object sender, RoutedEventArgs e)
{
PasswordWatermark.Opacity = 0;
pbPassword.Opacity = 100;
}
Hope it's help
I don't think we can put watermark in the Password control.
You can put a TextBox with wartermark in the same row and same column with the Password control, then handle the two controls' GotFocus and LostFocus events to make the control Visible or Collapsed.
There is no toolkit yet which provides watermarked password box. However this may help:-
http://code.msdn.microsoft.com/windowsdesktop/Watermarked-TextBox-and-444ebdec
Also, check out http://julmar.com/blog/mark/?p=300 for both a Textbox and PasswordBox implementation for WinRT.

Play audio file in the background in Windows8 app?

I am developing an app that displays information while at the same time plays audio file in the background. This is a Windows 8 tablet app. Any help or suggestion is helpful.
Thanks
In the XAML, you will have a MediaElement with the following attributes
<ContentControl x:Name="Host">
<MediaElement Source="Assets/myMusic.mp3" IsLooping="True" AutoPlay="True" AudioCategory="BackgroundCapableMedia" PosterSource="/Images/placeholder-sdk.png" VerticalAlignment="Center" HorizontalAlignment="Center" Name="myMediaElement" Height="350" Width="640" />
</ContentControl>
In the App bar, you can have some buttons for media control like
<Page.BottomAppBar>
<AppBar Opened="BottomAppBar_Opened" Background="#FF3C3838" x:Name="BottomAppBarColors" Padding="10,0,10,0" AutomationProperties.Name="Bottom App Bar Colors">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="LFullScreenLandscapeViewGrid" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
<Button x:Name="Play" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="Stop" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
<StackPanel Visibility="Collapsed" x:Name="PortraitViewGrid" Orientation="Horizontal" Grid.ColumnSpan="2" HorizontalAlignment="Right">
<Button x:Name="PauseCollapsed" Click="PauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" Tag="Pause"/>
<Button x:Name="PlayCollapsed" Click="PlayButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" Tag="Play"/>
<Button x:Name="StopCollapsed" Click="StopButton_Click" Style="{StaticResource StopAppBarButtonStyle}" Tag="Stop"/>
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
In the .CS code file, you can control the mediaelement object.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
If you have multiple screens that use the media and controls to play, pause and stop the media file, then it's better to write code for these events once and then use it from different screens. I have seen people writing the following code in every screen they use the media.
private void PauseButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Pause();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.Stop();
}
private void ForwardButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = 2.0;
}
private void RewindButton_Click(object sender, RoutedEventArgs e)
{
myMediaElement.DefaultPlaybackRate = 0.0;
myMediaElement.PlaybackRate = -1.0;
}
So instead of repeating the code in every screen, follow a good event pattern to reduce duplicate code
The best thing is to use MVVM pattern and you can find a sample at http://code.msdn.microsoft.com/windowsapps/Background-Audio-c-Metro-d2fc7719/view/SourceCode
So the design is to write up the code in one common place and fire them from the front end UI.
You need to use a MediaElement.