最近在学习微软的WPF,发现在wpf窗体居然没有类似java showInputDialog功能的简单对话框,只好自己做了,参照了window form的一些例子进行了改进,希望能对大家有帮助。
 
源码如下:
/*文件名:InputDialog.xaml.cs*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
 
namespace SCALCIDWPF.NewGUI
{
/// <summary>
/// Interaction logic for InputDialog.xaml
/// </summary>
public partial class InputDialog : Window
{
public Boolean _value_button;
public InputDialog()
{
InitializeComponent();
}
public Boolean value_button {
get { return _value_button; }
set { _value_button = value; }
}
public static Boolean Show(string text, string title, string defaultInput, out string userInput)
{
InputDialog dialog = new InputDialog();
dialog.lbText.Content = text;
dialog.Title = title;
dialog.tbInput.Text = defaultInput;
dialog.ShowDialog();
Boolean resultat = dialog.value_button;
userInput = dialog.tbInput.Text;
return resultat;
}
 
private void Window_Loaded(object sender, RoutedEventArgs e)
{
tbInput.Focus();
tbInput.SelectAll();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.value_button = true;
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.value_button = false;
this.Close();
}
 
 
}
}
 
 
/*文件名:InputDialog.xaml*/
<Window x:Class="SCALCIDWPF.NewGUI.InputDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="InputDialog" Height="127" Width="412" Loaded="Window_Loaded" Icon="/SCALCIDWPF;component/Image/HQ1.ico">
<Grid Height="90" Width="390">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="390*" />
<ColumnDefinition Width="0*" />
</Grid.ColumnDefinitions>
<Label Height="28" Margin="12,3,12,0" Name="lbText" VerticalAlignment="Top">question</Label>
<TextBox Margin="20,26,26,0" Name="tbInput" Height="29" VerticalAlignment="Top" />
<Button Height="23" HorizontalAlignment="Left" Margin="95,0,0,7" Name="btnOk" VerticalAlignment="Bottom" Width="75" Click="btnOk_Click">OK</Button>
<Button Height="23" Margin="193,0,125,7" Name="btnCancel" VerticalAlignment="Bottom" Click="btnCancel_Click">Annuler</Button>
</Grid>
</Window>
 
 
使用方法:
在按钮事件中调用即可:
private void btnAjouter_Click(object sender, RoutedEventArgs e)
{
string defaultResponse = "";
//You must also pass input box title, a default response, and
//input string
if(InputDialog.Show("Quel est le nom de la nouvelle ‚num‚ration?", "Input", "Enum", out defaultResponse) == true)
txtName.Text = defaultResponse;
}
 
 
附件里有源代码(压缩文件),另外使用时请修改你的namespace和相应的控件名字,本程序已在vs2008下编译测试无误!