凡在WPF中Button.DialogResult? [英] Where is Button.DialogResult in WPF?

查看:748
本文介绍了凡在WPF中Button.DialogResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.button.aspx\">System.Windows.Forms.Button有一个属性<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.button.dialogresult.aspx\">DialogResult,哪里是这个属性的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx\">System.Windows.Controls.Button (WPF)?

In System.Windows.Forms.Button there is a property DialogResult, where is this property in the System.Windows.Controls.Button (WPF)?

推荐答案

有没有内置Button.DialogResult,但你可以使用一个简单的附加的属性创建你自己的(如果你喜欢):

There is no built-in Button.DialogResult, but you can create your own (if you like) using a simple attached property:

public class ButtonHelper
{
  // Boilerplate code to register attached property "bool? DialogResult"
  public static bool? GetDialogResult(DependencyObject obj) { return (bool?)obj.GetValue(DialogResultProperty); }
  public static void SetDialogResult(DependencyObject obj, bool? value) { obj.SetValue(DialogResultProperty, value); }
  public static readonly DependencyProperty DialogResultProperty = DependencyProperty.RegisterAttached("DialogResult", typeof(bool?), typeof(ButtonHelper), new UIPropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
    {
      // Implementation of DialogResult functionality
      Button button = obj as Button;
      if(button==null)
          throw new InvalidOperationException(
            "Can only use ButtonHelper.DialogResult on a Button control");
      button.Click += (sender, e2) =>
      {
        Window.GetWindow(button).DialogResult = GetDialogResult(button);
      };
    }
  });
}

这将允许你写:

<Button Content="Click Me" my:ButtonHelper.DialogResult="True" />

和获得等同于的WinForms行为(点击按钮,使对话框关闭并返回指定的结果)

and get behavior equivalent to WinForms (clicking on the button causes the dialog to close and return the specified result)

这篇关于凡在WPF中Button.DialogResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆