Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer to take an approach using dialogs that doesn't lock up the application, and moves away from the more traditional Win32 Dialog.</p> <p><strong>Example</strong></p> <p><img src="https://i.stack.imgur.com/nNyo3.png" alt="Input Dialog"></p> <p><strong>Input Dialog Hidden</strong></p> <p><img src="https://i.stack.imgur.com/KMwaN.png" alt="Input Dialog not showing."></p> <p>In this example I use a simplified version of the <a href="http://en.wikipedia.org/wiki/Model_View_ViewModel" rel="noreferrer">MVVM</a> based solution I am using for my applications. It may not be pretty, but should give you a solid idea on the basics behind it.</p> <p><strong>The XAML:</strong></p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/&gt; &lt;ListBox x:Name="MyListBox"/&gt; &lt;/StackPanel&gt; &lt;!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! --&gt; &lt;Grid x:Name="InputBox" Visibility="Collapsed"&gt; &lt;Grid Background="Black" Opacity="0.5"/&gt; &lt;Border MinWidth="250" Background="Orange" BorderBrush="Black" BorderThickness="1" CornerRadius="0,55,0,55" HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;StackPanel&gt; &lt;TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" /&gt; &lt;TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/&gt; &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center"&gt; &lt;Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/&gt; &lt;Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>It's very easy to show this dialog as you only need to set the Visibility of the <code>InputBox</code> grid to visible. You then simply handle the Yes / No buttons and get the Input text from the TextBox. </p> <p>So instead of using code that requires <code>ShowDialog()</code>, you simply set the <code>Visibility</code> option to <code>Visible</code>. There are still some things to do in this example that we will handle in code-behind, like for example clearing the InputText box after handling the Yes/No Button clicks.</p> <p><strong>The code-behind:</strong></p> <pre><code>namespace WpfApplication1 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void CoolButton_Click(object sender, RoutedEventArgs e) { // CoolButton Clicked! Let's show our InputBox. InputBox.Visibility = System.Windows.Visibility.Visible; } private void YesButton_Click(object sender, RoutedEventArgs e) { // YesButton Clicked! Let's hide our InputBox and handle the input text. InputBox.Visibility = System.Windows.Visibility.Collapsed; // Do something with the Input String input = InputTextBox.Text; MyListBox.Items.Add(input); // Add Input to our ListBox. // Clear InputBox. InputTextBox.Text = String.Empty; } private void NoButton_Click(object sender, RoutedEventArgs e) { // NoButton Clicked! Let's hide our InputBox. InputBox.Visibility = System.Windows.Visibility.Collapsed; // Clear InputBox. InputTextBox.Text = String.Empty; } } } </code></pre> <p>The code-behind could easily be done using a Dependency, or as ViewModel logic in this case, but for simplicity I kept it in the code-behind.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload