Asked  7 Months ago    Answers:  5   Viewed   76 times

I lately had the problem of creating add and edit dialogs for my wpf app.

All I want to do in my code was something like this. (I mostly use viewmodel first approach with mvvm)

ViewModel which calls a dialog window:

var result = this.uiDialogService.ShowDialog("Dialogwindow Title", dialogwindowVM);
// Do anything with the dialog result

How does it work?

First, I created a dialog service:

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();

WindowDialog is a special but simple window. I need it to hold my content:

<Window x:Class="WindowDialog"
    WindowStartupLocation="CenterOwner" SizeToContent="WidthAndHeight">
    <ContentPresenter x:Name="DialogPresenter" Content="{Binding .}">


A problem with dialogs in wpf is the dialogresult = true can only be achieved in code. That's why I created an interface for my dialogviewmodel to implement it.

public class RequestCloseDialogEventArgs : EventArgs
    public bool DialogResult { get; set; }
    public RequestCloseDialogEventArgs(bool dialogresult)
        this.DialogResult = dialogresult;

public interface IDialogResultVMHelper
    event EventHandler<RequestCloseDialogEventArgs> RequestCloseDialog;

Whenever my ViewModel thinks it's time for dialogresult = true, then raise this event.

public partial class DialogWindow : Window
    // Note: If the window is closed, it has no DialogResult
    private bool _isClosed = false;

    public DialogWindow()
        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)

        d.RequestCloseDialog += new EventHandler<RequestCloseDialogEventArgs>
                                        eh => 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;

Now at least I have to create a DataTemplate in my resource file(app.xaml or something):

<DataTemplate DataType="{x:Type DialogViewModel:EditOrNewAuswahlItemVM}" >

Well thats all, I can now call dialogs from my viewmodels:

 var result = this.uiDialogService.ShowDialog("Dialogwindow Title", dialogwindowVM);

Now my question, do you see any problems with this solution?

Edit: for completeness. The ViewModel should implement IDialogResultVMHelper and then it can raise it within a OkCommand or something like this:

public class MyViewmodel : IDialogResultVMHelper
    private readonly Lazy<DelegateCommand> _okCommand;

    public MyViewmodel()
         this._okCommand = new Lazy<DelegateCommand>(() => 
             new DelegateCommand(() => 
                     new RequestCloseDialogEventArgs(true)), () => 
                         YourConditionsGoesHere = true));

    public ICommand OkCommand
        get { return this._okCommand.Value; } 

    public event EventHandler<RequestCloseDialogEventArgs> RequestCloseDialog;
    private void InvokeRequestCloseDialog(RequestCloseDialogEventArgs e)
        var handler = RequestCloseDialog;
        if (handler != null) 
            handler(this, e);

EDIT 2: I used the code from here to make my EventHandler register weak:
(Website no longer exists, WebArchive Mirror)

public delegate void UnregisterCallback<TE>(EventHandler<TE> eventHandler) 
    where TE : EventArgs;

public interface IWeakEventHandler<TE> 
    where TE : EventArgs
    EventHandler<TE> Handler { get; }

public class WeakEventHandler<T, TE> : IWeakEventHandler<TE> 
    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<TE> mHandler;
    private UnregisterCallback<TE> mUnregister;

    public WeakEventHandler(EventHandler<TE> eventHandler,
                                UnregisterCallback<TE> 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 = null;

    public EventHandler<TE> Handler
        get { return mHandler; }

    public static implicit operator EventHandler<TE>(WeakEventHandler<T, TE> weh)
        return weh.mHandler;

public static class EventHandlerUtils
    public static EventHandler<TE> MakeWeak<TE>(this EventHandler<TE> eventHandler, 
                                                    UnregisterCallback<TE> 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.",

        var wehType = typeof(WeakEventHandler<,>).MakeGenericType(
                          eventHandler.Method.DeclaringType, typeof(TE));

        var wehConstructor = wehType.GetConstructor(new Type[] 
                                 typeof(EventHandler<TE>), typeof(UnregisterCallback<TE>) 

        IWeakEventHandler<TE> weh = (IWeakEventHandler<TE>)wehConstructor.Invoke(
                                        new object[] { eventHandler, unregister });

        return weh.Handler;



This is a good approach and I used similar ones in the past. Go for it!

One minor thing I'd definitely do is make the event receive a boolean for when you need to set "false" in the DialogResult.

event EventHandler<RequestCloseEventArgs> RequestCloseDialog;

and the EventArgs class:

public class RequestCloseEventArgs : EventArgs
    public RequestCloseEventArgs(bool dialogResult)
        this.DialogResult = dialogResult;

    public bool DialogResult { get; private set; }
Tuesday, June 1, 2021
answered 7 Months ago

Your question really seems to be asking 2 questions:

  1. Where are some good tutorials on WPF, assuming I have no previous WPF experience?
  2. Where are some good tutorials on learning MVVM?

Some of these resources may be duplicated in previous answers...

Tutorials on WPF

  • A Guided Tour of WPF by Josh Smith

    I wrote a series of introductory WPF articles on The Code Project. The goal of those articles is to bring someone with no WPF experience up-to-speed enough so that (s)he can fully understand how the series’ demo application works.

  • Bea Stollnitz (link is to her archives) has a number of great articles on WPF.

  • WPF: A Beginner's Guide - Part 1 of n by Sacha Barber

  • WPF Training Videos

MVVM Tutorials

  • WPF Apps With The Model-View-ViewModel Design Pattern by Josh Smith (duplicate link already provided by Yacoder)

  • Jason Dolinger's presentation on the Model-View-ViewModel (link to video embedded in article)

  • Dan Crevier's DataModel-View-ViewModel pattern series (similar to MVVM)

Composite WPF (Prism) Resources
Though not exactly what you asked, it is the natural progression with WPF and MVVM.

  • Codeplex: Composite WPF (Prism)

  • Silver Bay Labs has a number of great videos on learning Prism

  • Channel 9: 10 step-by-step videos on using Silverlight and Prism

  • Codeplex: Introduction to Composite WPF (CAL, Prism): Part 1 (of n)

Tuesday, June 1, 2021
answered 7 Months ago

I suggest forgoing the 1990's modal dialogs and instead implementing a control as an overlay (canvas+absolute positioning) with visibility tied to a boolean back in the VM. Closer to an ajax type control.

This is very useful:

<BooleanToVisibilityConverter x:Key="booltoVis" />

as in:

<my:ErrorControl Visibility="{Binding Path=ThereWasAnError, Mode=TwoWay, Converter={StaticResource booltoVis}, UpdateSourceTrigger=PropertyChanged}"/>

Here's how I have one implemented as a user control. Clicking on the 'x' closes the control in a line of code in the usercontrol's code behind. (Since I have my Views in an .exe and ViewModels in a dll, I don't feel bad about code that manipulates UI.)

Wpf dialog

Tuesday, June 1, 2021
answered 7 Months ago

PEP 8 authoritatively states:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

PEP 8 should be the basis of any "in-house" style guide, since it summarizes what the core Python team has found to be the most effective style, overall (and with individual dissent of course, as on any other language, but consensus and the BDFL agree on PEP 8).

Wednesday, June 9, 2021
answered 6 Months ago

In the View, set the items source to a CollectionViewSource that itself binds to the Animals collection on the ViewModel. The CollectionViewSource can be grouped, something like this:

   <PropertyGroupDescription PropertyName="Continent" />

You'll also need to set a group style on whatever items control you've got - something like this

            <TextBlock FontWeight="Bold" FontSize="15"
               Text="{Binding Path=Name}"/>

Taken from the example on this page -

That's setting the HeaderTemplate, but if you play around a bit you should be able to set a container style for each group.

Hope this helps!

Update: I wasn't too sure about this so I had a quick bash at the code. Assuming 'toggle button' is 'radio button', this is what I've come up with:

        <CollectionViewSource x:Key="Animals" Source="{Binding}">
                <PropertyGroupDescription PropertyName="Continent" />

    <ItemsControl ItemsSource="{Binding Source={StaticResource Animals}}">
            <x:Static Member="GroupStyle.Default" />

                <RadioButton Content="{Binding Name}" GroupName="{Binding Continent}" />

In addition, you can display each group as a GroupBox by replacing the line <x:Static Member="GroupStyle.Default" /> with:

      <Style TargetType="{x:Type GroupItem}">
         <Setter Property="Template">
                  <GroupBox Margin="10" Header="{Binding Name}">
                        <ItemsPresenter />

However, the radio buttons will not be mutually exclusive on their own (I presume because they are wrapped in ListItem controls, or something else that makes them a single child of a grouping parent). That code was stolen/modified from the GroupStyle entry in MSDN if you want to go back for more information (their example had expanders to show/hide groups):

Let me know if I've missed the point at all.

Tuesday, November 2, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :