|
namespace DialogCloser.Behaviour
{
using System.Windows;
/// <summary>
/// Attachable behaviour class that can be used to tell a View (window) to close itself
/// when the ViewModel has determined that.
///
/// Usage in XAML and ViewModel is the following
///
/// --- ViewModel ---
/// private this.mDialogCloseResult = null;
///
/// public bool? DialogCloseResult
/// {
/// get
/// {
/// return this.mDialogCloseResult;
/// }
///
/// private set
/// {
/// if (this.mDialogCloseResult != value)
/// {
/// this.mDialogCloseResult = value;
/// this.NotifyPropertyChanged(() => this.DialogCloseResult);
/// }
/// }
/// }
///
/// --- XAML in View ---
///
/// <Window ...
/// xmlns:xc="clr-namespace:MsgBox.Internal.ViewModel"
/// xc:DialogCloser.DialogResult="{Binding DialogCloseResult}">
///
/// Source: http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form
/// </summary>
public static class DialogCloser
{
/// <summary>
/// Dependency property for attached behaviour in dialog windows.
/// This can be is used to close a dialog window via ViewModel.
/// </summary>
private static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached(
"DialogResult",
typeof(bool?),
typeof(DialogCloser),
new PropertyMetadata(DialogResultChanged));
/// <summary>
/// Setter of corresponding dependency property
/// </summary>
/// <param name="target"></param>
/// <param name="value"></param>
public static void SetDialogResult(Window target, bool? value)
{
target.SetValue(DialogResultProperty, value);
}
private static void DialogResultChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var window = d as Window;
if (window != null)
{
if (window.Visibility == Visibility.Visible)
{
// window.DialogResult = e.NewValue as bool?;
window.Close();
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed
Edi
and a few other projects on
GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.
https://de.linkedin.com/in/dirkbahle