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

WPF

 
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
MemberMember 104997162-Jan-14 6:53 
SuggestionRe: Convert xaml to C# Pin
BenScharbach14-Oct-17 9:45
MemberBenScharbach14-Oct-17 9:45 
QuestionWPF DialogService Question Pin
Kevin Marois29-Dec-13 11:31
professionalKevin Marois29-Dec-13 11:31 
I am creating a DialogService. I have looked at a bunch and don't like them for various reasons...

Assumptions

a) The serivce will maintain a registry of Views to ViewModels
b) The Views will always be Windows
c) The ViewModels will always inherit from _DialogBaseViewModel.

So here's what I have so far:

Interface
public interface IDialogService<TDialogType, TViewModelType> 
                    where TDialogType : Window 
                    where TViewModelType : _DialogBaseViewModel
{
    void RegisterView(TDialogType dialog, TViewModelType viewModel);
    bool? ShowDialog(TViewModelType viewModel);
}


Implementation
public class DialogService<TDialogType, TViewModelType> : IDialogService<TDialogType, TViewModelType>
    where TDialogType : Window
    where TViewModelType : _DialogBaseViewModel
{                   

    private Dictionary<TDialogType, TViewModelType> registry = new Dictionary<TDialogType, TViewModelType>();

    public void RegisterView(TDialogType dialogType, TViewModelType viewModelType)
    {
        bool exists = registry.Where(x => x.Key.GetType() == dialogType.GetType()).Any();

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

    public bool? ShowDialog(TViewModelType viewModel)
    {
        TDialogType dialog = registry.Where(x => x.Value == viewModel).Select(x => x.Key).FirstOrDefault();
        dialog.DataContext = viewModel;
        dialog.ShowDialog();
        bool? result = dialog.DialogResult;
        return result;
    }
}


Usage
public class TestIt
{
    public void Test()
    {
        DialogService<Window, _DialogBaseViewModel> service = new DialogService<Window, _DialogBaseViewModel>();

        service.RegisterView(new PersonView(), new PersonViewModel());
        service.RegisterView(new InvoiceView(), new InvoiceViewModel());
        service.RegisterView(new AnotherView(), new AnotherViewModel());

        bool? dialogResult = service.ShowDialog(new InvoiceViewModel());
    }
}


Problem
How do I change this so that I register types instead of concrete classes? If you look at the test you can see that I had to create instances to get it to work... This isn't right.

Thanks
If it's not broken, fix it until it is

AnswerRe: WPF DialogService Question Pin
veboys30-Dec-13 5:08
Memberveboys30-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 
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 

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.