WPF:使用模态对话框/InputDialog查询用户

发布于 2024-10-10 03:24:17 字数 206 浏览 8 评论 0原文

在 WPF 应用程序中,我必须从用户那里获取一行信息,但我不想使用模态对话框。然而,似乎没有为此预设的对话框。有什么简单易行的方法可以做到这一点。我发现尝试用许多版本的对话框等来找出这一点有点复杂。

我已经不得不使用 OpenFileDialog 和 SaveFileDialog。 Microsoft.Win32 和 System.Windows.Form 等版本之间有什么不同?

In a WPF application I have to get one line of info from user and I wan't to use a Modal Dialog. However there seems to be no preset dialog for this. What's a simple and easy way to do this. I find it somwhat complicated trying to find this out with the many versions of Dialogs and such.

Already I have had to use OpenFileDialog and SaveFileDialog. What is the different between version of these like Microsoft.Win32 and System.Windows.Form ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-10-17 03:24:17

在 WPF 中显示模式对话框不需要执行任何特殊操作。只需将 Window 添加到您的项目(假设类名称为 MyDialog),然后执行以下操作:

var dialog = new MyDialog();
dialog.ShowDialog();

Window.ShowDialog 负责显示以模态方式窗口。

示例:

public class MyDialog : Window {
    public MyDialog() {
        this.InitializeComponent();
        this.DialogResult = null;
    }

    public string SomeData { get; set; } // bind this to a control in XAML
    public int SomeOtherData { get; set; } // same for this

    // Attach this to the click event of your "OK" button
    private void OnOKButtonClicked(object sender, RoutedEventArgs e) {
        this.DialogResult = true;
        this.Close();
    }

    // Attach this to the click event of your "Cancel" button
    private void OnCancelButtonClicked(object sender, RoutedEventArgs e) {
        this.DialogResult = false;
        this.Close();
    }
}

在您的代码中的某处:

var dialog = new MyDialog();
// If MyDialog has properties that affect its behavior, set them here
var result = dialog.ShowDialog();

if (result == false) {
    // cancelled
}
else if (result == true) {
    // do something with dialog.SomeData here
}

There's nothing special you need to do to show a modal dialog in WPF. Just add a Window to your project (let's say the class name is MyDialog), and then do:

var dialog = new MyDialog();
dialog.ShowDialog();

Window.ShowDialog takes care of showing the window in a modal manner.

Example:

public class MyDialog : Window {
    public MyDialog() {
        this.InitializeComponent();
        this.DialogResult = null;
    }

    public string SomeData { get; set; } // bind this to a control in XAML
    public int SomeOtherData { get; set; } // same for this

    // Attach this to the click event of your "OK" button
    private void OnOKButtonClicked(object sender, RoutedEventArgs e) {
        this.DialogResult = true;
        this.Close();
    }

    // Attach this to the click event of your "Cancel" button
    private void OnCancelButtonClicked(object sender, RoutedEventArgs e) {
        this.DialogResult = false;
        this.Close();
    }
}

In your code somewhere:

var dialog = new MyDialog();
// If MyDialog has properties that affect its behavior, set them here
var result = dialog.ShowDialog();

if (result == false) {
    // cancelled
}
else if (result == true) {
    // do something with dialog.SomeData here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文