Click here to Skip to main content
15,883,883 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: WPF DialogService Question Pin
veboys30-Dec-13 5:08
veboys30-Dec-13 5:08 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 6:56
professionalKevin Marois30-Dec-13 6:56 
SuggestionRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 5:39
professionalRon Beyer30-Dec-13 5:39 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 6:56
professionalKevin Marois30-Dec-13 6:56 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 7:06
professionalKevin Marois30-Dec-13 7:06 
GeneralRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 7:22
professionalRon Beyer30-Dec-13 7:22 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 7:22
professionalKevin Marois30-Dec-13 7:22 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 7:35
professionalKevin Marois30-Dec-13 7:35 
Ok, so here's what I have now. Then a question..

Service
public static class DialogService
{
    private static Dictionary<Type, Type> registry = new Dictionary<Type, Type>();

    public static void RegisterDialog(Type dialogType, Type viewModelType)
    {
        if (!dialogType.IsSubclassOf(typeof(Window)))
        {
            throw new ArgumentException("The dialogType must be a subclass of type Window.");
        }

        bool exists = registry.Where(x => x.Key == dialogType).Any();

        if (!exists)
        {
            registry.Add(dialogType, viewModelType);
        }
    }

    public static bool? ShowDialog<TViewModelType>(TViewModelType viewModel) where TViewModelType : _DialogBaseViewModel
    {
        Type dialogType = registry.Where(x => x.Value == viewModel.GetType()).Select(x => x.Key).FirstOrDefault();
        var dialog = (Window)Activator.CreateInstance(dialogType);

        dialog.DataContext = viewModel;
        dialog.ShowDialog();
            
        bool? result = dialog.DialogResult;

        return result;
    }
}


Usage
DialogService.RegisterDialog(typeof(PersonView), typeof(PersonViewModel));
bool? dialogResult = DialogService.ShowDialog(new PersonViewModel());


The reason I'm forcing the VM to inherit from _DialogBaseViewModel is that _DialogBaseViewModel has a property called DialogResult. I have a DP that is set in a window's header XAML that sets the DialogResult from the VM and closes the window. Works real nice:

<Window x:Class="Osprey.UI.WPF.Views.MyDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:behaviors="clr-namespace:Osprey.UI.WPF.Classes.Behaviors"
        Title="My Dialog" 
        xmlns:xc="clr-namespace:Osprey.UI.WPF.Classes"
        xc:DialogCloser.DialogResult="{Binding DialogResult}">


Problem
How would you return an object from this service?

Assume a dialog prompts the user to enter some data. That data could be stored to a model. I would want the service to return the model if DialogResult was true.

My first thought was to have a static wrapper class around this service that has methods for each type I would want to return. If null is returned then the dialog's DialogResult was false.

What do you think?
If it's not broken, fix it until it is

GeneralRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 7:46
professionalRon Beyer30-Dec-13 7:46 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 7:53
professionalKevin Marois30-Dec-13 7:53 
GeneralRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 8:03
professionalRon Beyer30-Dec-13 8:03 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 8:08
professionalKevin Marois30-Dec-13 8:08 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 8:45
professionalKevin Marois30-Dec-13 8:45 
GeneralRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 8:54
professionalRon Beyer30-Dec-13 8:54 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 9:18
professionalKevin Marois30-Dec-13 9:18 
GeneralRe: WPF DialogService Question Pin
Ron Beyer30-Dec-13 10:25
professionalRon Beyer30-Dec-13 10:25 
GeneralRe: WPF DialogService Question Pin
Kevin Marois30-Dec-13 10:28
professionalKevin Marois30-Dec-13 10:28 
GeneralRe: WPF DialogService Question Pin
Kevin Marois31-Dec-13 5:54
professionalKevin Marois31-Dec-13 5:54 
GeneralRe: WPF DialogService Question Pin
Ron Beyer31-Dec-13 6:09
professionalRon Beyer31-Dec-13 6:09 
GeneralRe: WPF DialogService Question Pin
Mycroft Holmes6-Jan-14 21:57
professionalMycroft Holmes6-Jan-14 21:57 
QuestionConvert BitmapImage into byte arrays in wpf Pin
Member 1001614027-Dec-13 0:23
Member 1001614027-Dec-13 0:23 
AnswerRe: Convert BitmapImage into byte arrays in wpf Pin
Pete O'Hanlon27-Dec-13 0:58
mvePete O'Hanlon27-Dec-13 0:58 
GeneralRe: Convert BitmapImage into byte arrays in wpf Pin
Member 1001614027-Dec-13 3:44
Member 1001614027-Dec-13 3:44 
QuestionWPF App Content Presenter in Main Window Pin
Kevin Marois23-Dec-13 7:21
professionalKevin Marois23-Dec-13 7:21 
AnswerRe: WPF App Content Presenter in Main Window Pin
SledgeHammer0123-Dec-13 10:22
SledgeHammer0123-Dec-13 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.