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

WPF

 
Questionupdate linq query result Pin
amigoface5-Jan-14 3:12
amigoface5-Jan-14 3:12 
QuestionLearn Wpf Pin
Member 105030394-Jan-14 2:00
Member 105030394-Jan-14 2:00 
AnswerRe: Learn Wpf Pin
Peter Leow4-Jan-14 2:16
professionalPeter Leow4-Jan-14 2:16 
AnswerRe: Learn Wpf Pin
Richard MacCutchan4-Jan-14 2:34
mveRichard MacCutchan4-Jan-14 2:34 
QuestionData Template Checkbox Binding Question Pin
Kevin Marois2-Jan-14 7:16
professionalKevin Marois2-Jan-14 7:16 
AnswerRe: Data Template Checkbox Binding Question Pin
Wayne Gaylard2-Jan-14 19:21
professionalWayne Gaylard2-Jan-14 19:21 
GeneralRe: Data Template Checkbox Binding Question Pin
Kevin Marois10-Jan-14 10:50
professionalKevin Marois10-Jan-14 10:50 
GeneralRe: Data Template Checkbox Binding Question Pin
Wayne Gaylard11-Jan-14 4:43
professionalWayne Gaylard11-Jan-14 4:43 
QuestionConvert xaml to C# Pin
Member 104997162-Jan-14 6:53
Member 104997162-Jan-14 6:53 
SuggestionRe: Convert xaml to C# Pin
BenScharbach14-Oct-17 9:45
BenScharbach14-Oct-17 9:45 
QuestionWPF DialogService Question Pin
Kevin Marois29-Dec-13 11:31
professionalKevin Marois29-Dec-13 11:31 
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 

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.