system.Windows。形式。按钮 有一个属性 对拨号, ,这个属性在哪里 system.Windows。控件。按钮 (WPF)?

有帮助吗?

解决方案

没有内置的按钮。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);
      };
    }
  });
}

这将使您写作:

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

并获得与Winforms相等的行为(单击按钮会导致对话框关闭并返回指定的结果)

其他提示

没有 Button.DialogResult 在WPF中。您只需要设置 DialogResultWindow 对还是错:

private void buttonOK_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

只需确保您已使用 ShowDialog 而不是 Show. 。如果您做后者,您会得到以下例外:

InvalidOperationException没有得到治疗

只有在创建窗口并显示为对话框之后才能设置Dialogresult。

MessageBoxResult result = MessageBox.Show("","");

if (result == MessageBoxResult.Yes)
{
// CODE IN HERE
}
else 
{
// CODE IN HERE
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top