Window.ShowDialog() 返回值為bool?。希望在窗口點擊OK時返回True。
解決方法:
ShowDialog()的注釋為:
// Returns:
// A System.Nullable value of type System.Boolean that specifies whether
// the activity was accepted (true) or canceled (false). The return value is
// the value of the System.Windows.Window.DialogResult property before a window
// closes.
DialogResult屬性的注釋:
// Summary:
// Gets or sets the dialog result value, which is the value that is returned
// from the System.Windows.Window.ShowDialog() method.
//
// Returns:
// A System.Nullable value of type System.Boolean. The default is false.
//...
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[TypeConverter(typeof(DialogResultConverter))]
public bool? DialogResult { get; set; }
所以將DialogResult = true;則會使ShowDialog()的返回值為true。
注:
當給DialogResult賦值之后會自動調用Close()。但是沒有找到文檔確切說明這點的。。。對于Cancel按鈕,如果不需要其他功能,可直接在xaml中設置IsCancel="True",DialogResult為False。如果IsDefault=“True"可以用回車鍵click button,但依然需要在click event中設置DialogResult。