WPF Tips: Window.ShowDialog() 返回 true

Window.ShowDialog() 返回值为bool?。希望在窗口点击OK时返回True。

解决方法:

ShowDialog()的注释为:

// Returns:
// A System.Nullable<T> 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<T> 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。

 

posted @ 2016-03-28 18:28  Jane&Coding  阅读(3503)  评论(0)    收藏  举报