Ray*_*rns 32

没有内置的Button.DialogResult,但您可以使用简单的附加属性创建自己的(如果您愿意):

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);
      };
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

这将允许你写:

<Button Content="Click Me" my:ButtonHelper.DialogResult="True" />
Run Code Online (Sandbox Code Playgroud)

并获得等同于WinForms的行为(单击该按钮会导致对话框关闭并返回指定的结果)


Tho*_*que 20

Button.DialogResultWPF中没有.你只需要设置DialogResultWindow为true或false:

private void buttonOK_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}
Run Code Online (Sandbox Code Playgroud)