私は、アプリケーションをロックアップしないダイアログを使用し、より伝統的なWin32ダイアログから離れてアプローチする方が好きです。この例では
例
入力ダイアログ隠し
私は私のアプリケーションのために使用していますMVVMベースのソリューションの簡易版を使用します。それはかなりではないかもしれませんが、その背後にある基本についての確固たるアイデアを与えるべきです。
XAML:
<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">
<Grid>
<StackPanel>
<Button Content="Cool Button" x:Name="CoolButton" Click="CoolButton_Click"/>
<ListBox x:Name="MyListBox"/>
</StackPanel>
<!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
<Grid x:Name="InputBox" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="Orange"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,55,0,55"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<TextBlock Margin="5" Text="Input Box:" FontWeight="Bold" FontFamily="Cambria" />
<TextBox MinWidth="150" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="InputTextBox"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="YesButton" Margin="5" Content="Yes" Background="{x:Null}" Click="YesButton_Click"/>
<Button x:Name="NoButton" Margin="5" Content="No" Background="{x:Null}" Click="NoButton_Click" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</Grid>
それはあなただけ見えにInputBox
グリッドの可視性を設定する必要があるとして、このダイアログを表示することは非常に簡単です。 Yes/Noボタンを操作し、TextBoxから入力テキストを取得するだけです。
ShowDialog()
が必要なコードを使用する代わりに、Visibility
オプションをVisible
に設定するだけです。この例では、Yes/Noボタンのクリックを処理した後にInputTextボックスをクリアするなど、コードビハインドで処理するいくつかのことがまだあります。
コードビハインド:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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;
}
}
}
コードビハインドを容易に依存関係を使用して行うことができ、又はこの場合のビューモデルのロジックとして、簡単のため、私はコードビハインドでそれを保ちます。
http://social.msdn.microsoft.com/Forums/en/winforms/thread/191ddf61-3ae5-4845-b852-56bb9b77238aを参照してください。 – mjwills