In System.Windows.Forms.Button there is a property DialogResult, where is this property in the System.Windows.Controls.Button (WPF)?
Asked
Active
Viewed 1.8k times
4 Answers
33
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)

Ray Burns
- 62,163
- 12
- 140
- 141
-
I learned new stuff in herer, this attatching, eventho im not going to use it in this case, sure will be useful! thanks a lot – Shimmy Weitzhandler Nov 19 '09 at 02:41
-
I never knew about the GetWindow func, that's just amazing! – Shimmy Weitzhandler Nov 19 '09 at 02:46
-
A great solution, made greater in its simplicity. – David Keaveny May 25 '12 at 03:52
23
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;
}

Thomas Levesque
- 286,951
- 70
- 623
- 758
-4
MessageBoxResult result = MessageBox.Show("","");
if (result == MessageBoxResult.Yes)
{
// CODE IN HERE
}
else
{
// CODE IN HERE
}

McGarnagle
- 101,349
- 31
- 229
- 260

VARO
- 1
- 1
-
This code won't even work... `MessageBox.Show("", "");` will not show Yes|No buttons. – qJake Jan 02 '13 at 00:07