Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Button.DialogResult in WPF?

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

like image 916
Shimmy Weitzhandler Avatar asked Nov 18 '09 21:11

Shimmy Weitzhandler


2 Answers

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);
      };
    }
  });
}

This will allow you to write:

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

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

like image 148
Ray Burns Avatar answered Oct 20 '22 19:10

Ray Burns


There is no Button.DialogResult in WPF. You just have to set the DialogResult of the Window to true or false :

private void buttonOK_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}
like image 39
Thomas Levesque Avatar answered Oct 20 '22 19:10

Thomas Levesque