A newbie question, but I can't get it to work. In trying to use the answer to SO "How Should the View Model Close the form" , I have a UserControl defined with:
<UserControl
.....
h:DialogCloser.DialogResult="{Binding DialogResult}"
>
which while designing shows:
Property 'DialogResult' is not attachable to elements of type 'UserControl'.
The DialogCloser is defined as:
public static class DialogCloser
{
public static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached(
"DialogResult",
typeof(bool?),
typeof(DialogCloser),
new PropertyMetadata(DialogResultChanged));
private static void DialogResultChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var window = d as Window;
if (window != null)
window.DialogResult = e.NewValue as bool?;
}
public static void SetDialogResult(Window target, bool? value)
{
target.SetValue(DialogResultProperty, value);
}
}
The UserControl is opened by:
var win = new WindowDialog();
win.Title = title;
win.DataContext = datacontext;
return win.ShowDialog();
In the View Model for the UserControl, I have:
public new void DoExit()
{
DialogResult = true;
}
private bool? dialogresult;
public bool? DialogResult
{
get
{
return dialogresult;
}
set
{
if (dialogresult != value)
{
dialogresult = value;
OnPropertyChanged("DialogResult");
}
}
}
When using DoExit(), the modal dialog, my UserControl, does not close. What's wrong? And how do I get the designer (VS 2010) to not throw error?
Thanks is advance for any help.
Addendum:
public class ModalDialogService : IModalDialogService
{
public bool? ShowDialog(string title, object datacontext)
{
var win = new WindowDialog();
win.Title = title;
win.DataContext = datacontext;
return win.ShowDialog();
}
}
Note: If the "UserControl" is rather made as a "Window", the designer is happy, but then the error is:
"Window must be the root of the tree. Cannot add Window as a child of Visual."
Help somebody?