Note that there are some explanatory texts on larger screens.

plurals
  1. POGood or bad practice for Dialogs in wpf with MVVM?
    text
    copied!<p>I lately had the problem of creating add and edit dialogs for my wpf app.</p> <p>All I want to do in my code was something like this. (I mostly use viewmodel first approach with mvvm)</p> <p>ViewModel which calls a dialog window:</p> <pre><code>var result = this.uiDialogService.ShowDialog("Dialogwindow Title", dialogwindowVM); // Do anything with the dialog result </code></pre> <p>How does it work?</p> <p>First, I created a dialog service:</p> <pre><code>public interface IUIWindowDialogService { bool? ShowDialog(string title, object datacontext); } public class WpfUIWindowDialogService : IUIWindowDialogService { public bool? ShowDialog(string title, object datacontext) { var win = new WindowDialog(); win.Title = title; win.DataContext = datacontext; return win.ShowDialog(); } } </code></pre> <p><code>WindowDialog</code> is a special but simple window. I need it to hold my content:</p> <pre><code>&lt;Window x:Class="WindowDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Title="WindowDialog" WindowStyle="SingleBorderWindow" WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight"&gt; &lt;ContentPresenter x:Name="DialogPresenter" Content="{Binding .}"&gt; &lt;/ContentPresenter&gt; &lt;/Window&gt; </code></pre> <p>A problem with dialogs in wpf is the <code>dialogresult = true</code> can only be achieved in code. That's why I created an interface for my <code>dialogviewmodel</code> to implement it. </p> <pre><code>public class RequestCloseDialogEventArgs : EventArgs { public bool DialogResult { get; set; } public RequestCloseDialogEventArgs(bool dialogresult) { this.DialogResult = dialogresult; } } public interface IDialogResultVMHelper { event EventHandler&lt;RequestCloseDialogEventArgs&gt; RequestCloseDialog; } </code></pre> <p>Whenever my ViewModel thinks it's time for <code>dialogresult = true</code>, then raise this event.</p> <pre><code>public partial class DialogWindow : Window { // Note: If the window is closed, it has no DialogResult private bool _isClosed = false; public DialogWindow() { InitializeComponent(); this.DialogPresenter.DataContextChanged += DialogPresenterDataContextChanged; this.Closed += DialogWindowClosed; } void DialogWindowClosed(object sender, EventArgs e) { this._isClosed = true; } private void DialogPresenterDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { var d = e.NewValue as IDialogResultVMHelper; if (d == null) return; d.RequestCloseDialog += new EventHandler&lt;RequestCloseDialogEventArgs&gt; (DialogResultTrueEvent).MakeWeak( eh =&gt; d.RequestCloseDialog -= eh;); } private void DialogResultTrueEvent(object sender, RequestCloseDialogEventArgs eventargs) { // Important: Do not set DialogResult for a closed window // GC clears windows anyways and with MakeWeak it // closes out with IDialogResultVMHelper if(_isClosed) return; this.DialogResult = eventargs.DialogResult; } } </code></pre> <p>Now at least I have to create a <code>DataTemplate</code> in my resource file(<code>app.xaml</code> or something):</p> <pre><code>&lt;DataTemplate DataType="{x:Type DialogViewModel:EditOrNewAuswahlItemVM}" &gt; &lt;DialogView:EditOrNewAuswahlItem/&gt; &lt;/DataTemplate&gt; </code></pre> <p>Well thats all, I can now call dialogs from my viewmodels:</p> <pre><code> var result = this.uiDialogService.ShowDialog("Dialogwindow Title", dialogwindowVM); </code></pre> <p>Now my question, do you see any problems with this solution?</p> <p>Edit: for completeness. The ViewModel should implement <code>IDialogResultVMHelper</code> and then it can raise it within a <code>OkCommand</code> or something like this:</p> <pre><code>public class MyViewmodel : IDialogResultVMHelper { private readonly Lazy&lt;DelegateCommand&gt; _okCommand; public MyViewmodel() { this._okCommand = new Lazy&lt;DelegateCommand&gt;(() =&gt; new DelegateCommand(() =&gt; InvokeRequestCloseDialog( new RequestCloseDialogEventArgs(true)), () =&gt; YourConditionsGoesHere = true)); } public ICommand OkCommand { get { return this._okCommand.Value; } } public event EventHandler&lt;RequestCloseDialogEventArgs&gt; RequestCloseDialog; private void InvokeRequestCloseDialog(RequestCloseDialogEventArgs e) { var handler = RequestCloseDialog; if (handler != null) handler(this, e); } } </code></pre> <p>EDIT 2: I used the code from here to make my EventHandler register weak:<br> <a href="http://diditwith.net/2007/03/23/SolvingTheProblemWithEventsWeakEventHandlers.aspx" rel="noreferrer">http://diditwith.net/2007/03/23/SolvingTheProblemWithEventsWeakEventHandlers.aspx</a><br> (Website no longer exists, <a href="http://web.archive.org/web/20140319163345/http://diditwith.net/2007/03/23/SolvingTheProblemWithEventsWeakEventHandlers.aspx" rel="noreferrer">WebArchive Mirror</a>)</p> <pre><code>public delegate void UnregisterCallback&lt;TE&gt;(EventHandler&lt;TE&gt; eventHandler) where TE : EventArgs; public interface IWeakEventHandler&lt;TE&gt; where TE : EventArgs { EventHandler&lt;TE&gt; Handler { get; } } public class WeakEventHandler&lt;T, TE&gt; : IWeakEventHandler&lt;TE&gt; where T : class where TE : EventArgs { private delegate void OpenEventHandler(T @this, object sender, TE e); private readonly WeakReference mTargetRef; private readonly OpenEventHandler mOpenHandler; private readonly EventHandler&lt;TE&gt; mHandler; private UnregisterCallback&lt;TE&gt; mUnregister; public WeakEventHandler(EventHandler&lt;TE&gt; eventHandler, UnregisterCallback&lt;TE&gt; unregister) { mTargetRef = new WeakReference(eventHandler.Target); mOpenHandler = (OpenEventHandler)Delegate.CreateDelegate( typeof(OpenEventHandler),null, eventHandler.Method); mHandler = Invoke; mUnregister = unregister; } public void Invoke(object sender, TE e) { T target = (T)mTargetRef.Target; if (target != null) mOpenHandler.Invoke(target, sender, e); else if (mUnregister != null) { mUnregister(mHandler); mUnregister = null; } } public EventHandler&lt;TE&gt; Handler { get { return mHandler; } } public static implicit operator EventHandler&lt;TE&gt;(WeakEventHandler&lt;T, TE&gt; weh) { return weh.mHandler; } } public static class EventHandlerUtils { public static EventHandler&lt;TE&gt; MakeWeak&lt;TE&gt;(this EventHandler&lt;TE&gt; eventHandler, UnregisterCallback&lt;TE&gt; unregister) where TE : EventArgs { if (eventHandler == null) throw new ArgumentNullException("eventHandler"); if (eventHandler.Method.IsStatic || eventHandler.Target == null) throw new ArgumentException("Only instance methods are supported.", "eventHandler"); var wehType = typeof(WeakEventHandler&lt;,&gt;).MakeGenericType( eventHandler.Method.DeclaringType, typeof(TE)); var wehConstructor = wehType.GetConstructor(new Type[] { typeof(EventHandler&lt;TE&gt;), typeof(UnregisterCallback&lt;TE&gt;) }); IWeakEventHandler&lt;TE&gt; weh = (IWeakEventHandler&lt;TE&gt;)wehConstructor.Invoke( new object[] { eventHandler, unregister }); return weh.Handler; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload