Programmatically Click PrimaryButton of ContentDialog - c#

In UWP, How can you Programmatically click the PrimaryButton of ContentDialog when KeyDown event of a textbox detects that the Enter has been pressed? Just trying to add a keyboard short cut to accept the answer In textbox and close the dialogbox all from the keyboard without needing to move the mouse and click ok.
private void A1TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
// Programmatically Click PrimaryButton Here
}
}
<ContentDialog
x:Class="App1.ContentDialog1"
Title="DialogBox"
Loaded="ContentDialog_Loaded"
PrimaryButtonText="Ok"
SecondaryButtonText="Cancel"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
<Grid Name="A1Grid">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Name="A1TextBox" Grid.Row="0"
PlaceholderText="Search String"
TextChanged="A1TextBox_TextChanged"
KeyDown="A1TextBox_KeyDown"/>
<ScrollViewer Grid.Row="1"
ScrollViewer.VerticalScrollBarVisibility="Auto"
VerticalAlignment="Stretch">
<ListBox Name="A1ListBox" MinHeight="200"/>
</ScrollViewer>
</Grid>
</ContentDialog>

Use following Code:
RoutedEventArgs f;
private void A1TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
ContentDialog_PrimaryButtonClick (Sender, f);
}
}
than he will fire the PrimaryButtonClick Event.

Have you tried making the button you want to click the "AcceptButton"?
//This goes in the form loading method
this.AcceptButton = PrimaryButton;
This should turn the button blue, and when pressing enter on the form will result in the click function being called.project
Edit: Just noticed this isn't for winforms.
this.DefaultButton = PrimaryButton;
Thanks Bill!

The UWP ContentDialog documentation states:
"You may optionally choose to differentiate one of the three buttons as the dialog's default button. Use the DefaultButton property to differentiate one of the buttons. This button will receive the Accent Button visual treatment, respond to the ENTER key automatically, and receive focus when the Dialog is opened unless the dialog's content contains focusable elements."
Change ContentDialog as follows:
<ContentDialog … DefaultButton="Primary">

Related

Split one button into two separate buttons after a click event in WPF

I am just curious if it is possible to replace a button into two buttons which the two buttons occupy the place of the original single buttons after a click event? Also after the click event of one of the two buttons, is it possible to revert to the original view where there is only one button?
So it looks like the fission of one button into two buttons and then the fusion of two buttons into one button again.
welcome to StackOverflow. Yes, it is quite possible, and in the simplest form, not even that difficult. It goes something like this:
Make a UI with 3 buttons.
Hide two of the, leave the last one visible.
On click of the visible button, make the other two buttons visible and hide the clicked button.
On click of one of the two buttons, revert the above procedure.
Of course, if you want to add stuff like animations and so, it will be a lot more complicated.
in .xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btn_big" Grid.ColumnSpan="2" Click="Click_big"/>
<Button x:Name="btn_small1" Grid.Column="1" Visibility="Hidden" Click="Click_small"/>
<Button x:Name="btn_small2" Grid.Column="2" Visibility="Hidden" Click="Click_small"/>
</Grid>
in .cs
private void Click_big(object sender, MouseButtonEventArgs e)
{
btn_big.Visibility=Visibility.Hidden;
btn_small1.Visibility=Visibility.Visible;
btn_small2.Visibility=Visibility.Visible;
}
private void Click_small(object sender, MouseButtonEventArgs e)
{
btn_big.Visibility=Visibility.Visible;
btn_small1.Visibility=Visibility.Hidden;
btn_small2.Visibility=Visibility.Hidden;
}

WPF focus DataGrid during GotFocus event of another control problem

According to the asnwer to Event for when KeyboardNavigation.TabNavigation Cycle occurs, the go-to solution is to add invisible control as last TabIndex of a Detail focus scope, handling GotFocus() on this dummy element. As part of handling this 'event' I would like to move focus back to master grid MasterDG.Focus():
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<DataGrid Name="MasterDG" ItemsSource="{Binding Items}" FocusManager.IsFocusScope="True"/>
<StackPanel Name="Detail" Grid.Row="1" FocusManager.IsFocusScope="True">
<TextBox/>
<TextBox/>
<TextBox/>
<Control Name="DummyControl"
GotFocus="DummyControl_GotFocus"/>
</StackPanel>
</Grid>
Event handler
private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
Save(); //save when done editing last element of detail
MasterDG.Focus();
}
However this causes not only MasterDG to be focused but also enter Edit mode on current cell and insert \t character overwriting any cell content. How can I fix the issue?
Note the actual contents of Detail are dynamically generated.
An easy workaround would be to call Focus() in the next dispatcher cycle:
private void DummyControl_GotFocus(object sender, RoutedEventArgs e)
{
Save(); //save when done editing last element of detail
Dispatcher.BeginInvoke(new Action(() => MasterDG.Focus()));
}

UWP - How to prevent InputPane from showing and hiding automatically

I want to manually control the behavior of InputPane to prevent it from showing or hiding automatically.
In my page that I put its image top, I want to InputPane show as user navigate to the page and keep showing until he/she clicks on specified button and prevent it from hiding if user clicks anywhere else in the page.
Also I want to InputPane remain hidden even if user clicks on TextBox.
I already know that there are TryShow() and TryHide(), but i can't revent auto showing and hiding.
The easy way to control it is by controlling focus of your TextBox. If you set IsTabStop on the TextBox to false - it won't take focus and so the SIP won't show up. If it already has focus - you'll need to move it out. If you want to display the SIP - focus the TextBox. Note that for performance reasons and also to prevent user confusion - it might make sense to use a TextBlock instead of a TextBox when the control should not be editable.
XAML
<Page
x:Class="App18.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App18"
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}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<TextBox
x:Name="myTextBox"
IsTabStop="False"
AcceptsReturn="True"
VerticalAlignment="Stretch"
TextChanged="MyTextBox_OnTextChanged"/>
<Button
x:Name="myButton"
Grid.Row="1"
Click="ButtonBase_OnClick">Edit</Button>
</Grid>
</Page>
C#
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App18
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
myTextBox.IsTabStop = true;
myTextBox.Focus(FocusState.Programmatic);
}
private void MyTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text.ToLower().Contains("done"))
{
myTextBox.IsTabStop = false;
myButton.Focus(FocusState.Programmatic);
}
}
}
}

C# WPF XAML In Window Messages Boxes

I am new to C# WPF, I have created a Popup window which contains a DataGrid to hold some data. In certain cases if there are any errors in the data I want to display this error in the same popup window at the bottom of the window (See screenshot). The idea is that the user can then click ok and the message will disappear displaying the full datagrid again.
Does anybody know how to do this?
I do not want another popup message box in a separate window, I want all messages to be displayed/stacked in the same pop window as the datagrid.
Below, there are 2 grids in the XAML: There is is the master grid, which contains the DataGrid, and another grid which contains the error UI. The error grid is normally collapsed (Visibility set to Visibility.Collapsed).
When the error message needs to be shown, the error grid's Visibility is set to Visibility.Visible, which shows the grid. When the user clicks on the "Dismiss" button, the error grid's Visibility is set to Visibility.Collapsed.
There is not a separate window or popup. Everything is contained within the master view.
XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<DataGrid>
</DataGrid>
<Button Grid.Column="1" Content="Show Message Window" VerticalAlignment="Center" Click="Button_Click_1"/>
<!-- This is the "error grid"-->
<Grid VerticalAlignment="Bottom" Height="Auto"
Background="AliceBlue" Visibility="Collapsed" Name="grdError">
<TextBlock Text="Oops. This is an error!"/>
<Button Content="Dismiss" HorizontalAlignment="Right" Click="Button_Click_3"/>
</Grid>
</Grid>
Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
grdError.Visibility = Visibility.Visible;
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
grdError.Visibility = Visibility.Collapsed;
}
}

Microsoft Media Platform Player Framework, make video fullscreen from button press (or double clicking on video)

I just started playing around with windows 8 development and I'm trying to figure out how to make a video whose source is from a url pop to full screen from either a button press or by double clicking on the area where the video is playing. Any idea on how to do that? As a followup, i'd also have to be able to minimize it back to it's normal playing window. Any ideas on how to do this using xaml C#?
reference:
http://playerframework.codeplex.com/wikipage?title=Windows%208%20Metro%20Player:%20Install%20and%20configure%20-%20XAML/C
The player framework has a boolean property called IsFullScreen to manage the fullscreen state. However, you need to do the work yourself to hide extra elements on the page and/or resize the mediaplayer. The recommended approach is to set this property and handle the IsFullScreenChanged event. For example:
<Grid Style="{StaticResource LayoutRootStyle}" x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="LeftPanel" Background="Red" Width="40"/>
<mmppf:MediaPlayer x:Name="player" Grid.Column="1" IsFullScreenVisible="True" Source="http://smf.blob.core.windows.net/samples/videos/wildlife.mp4"/>
</Grid>
public MainPage()
{
this.InitializeComponent();
player.DoubleTapped += player_DoubleTapped;
player.IsFullScreenChanged += player_IsFullScreenChanged;
}
void player_IsFullScreenChanged(object sender, RoutedPropertyChangedEventArgs<bool> e)
{
LeftPanel.Visibility = e.NewValue ? Visibility.Collapsed : Visibility.Visible;
}
void player_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
player.IsFullScreen = !player.IsFullScreen;
}
Note: there is also a property on MediaPlayer call IsFullScreenVisible that you can set to true to show the fullscreen toggle button in the default control strip.
I used this code for fullWindow. It works but in full window it uses default transportcontrols
private void MediaPlayer_IsFullScreenChanged(object sender, Windows.UI.Xaml.RoutedPropertyChangedEventArgs<bool> e)
{
Microsoft.PlayerFramework.MediaPlayer mp = (sender as Microsoft.PlayerFramework.MediaPlayer);
mp.IsFullWindow = !mp.IsFullWindow;
}

Categories

Resources