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

WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 3:09
cofounderChris Maunder16-Jul-09 3:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 3:05
cofounderChris Maunder16-Jul-09 3:05 
QuestionDevelopment Pin
KenCleve3-Jun-22 10:46
MemberKenCleve3-Jun-22 10:46 
AnswerRe: Development Pin
Gerry Schmitz4-Jun-22 10:35
mveGerry Schmitz4-Jun-22 10:35 
GeneralRe: Development Pin
KenCleve6-Jun-22 4:22
MemberKenCleve6-Jun-22 4:22 
QuestionTreeView in ControlTemplate - Handling Item Expanded Pin
Kevin Marois27-Apr-22 9:46
professionalKevin Marois27-Apr-22 9:46 
QuestionWPF DataGridCell Binding Problem Pin
Kevin Marois7-Apr-22 14:39
professionalKevin Marois7-Apr-22 14:39 
AnswerRe: WPF DataGridCell Binding Prolem Pin
Richard Deeming7-Apr-22 21:36
mveRichard Deeming7-Apr-22 21:36 
GeneralRe: WPF DataGridCell Binding Prolem Pin
Kevin Marois8-Apr-22 5:38
professionalKevin Marois8-Apr-22 5:38 
GeneralRe: WPF DataGridCell Binding Prolem Pin
Kevin Marois13-Apr-22 7:59
professionalKevin Marois13-Apr-22 7:59 
GeneralRe: WPF DataGridCell Binding Prolem Pin
Richard Deeming18-Apr-22 21:45
mveRichard Deeming18-Apr-22 21:45 
QuestionCode Behind Data Grid Style Issue Pin
Kevin Marois6-Apr-22 15:01
professionalKevin Marois6-Apr-22 15:01 
SuggestionRe: Code Behind Data Grid Style Issue Pin
Richard Deeming6-Apr-22 22:37
mveRichard Deeming6-Apr-22 22:37 
GeneralRe: Code Behind Data Grid Style Issue Pin
Kevin Marois7-Apr-22 5:25
professionalKevin Marois7-Apr-22 5:25 
AnswerRe: Code Behind Data Grid Style Issue Pin
Gerry Schmitz7-Apr-22 6:42
mveGerry Schmitz7-Apr-22 6:42 
GeneralRe: Code Behind Data Grid Style Issue Pin
Kevin Marois7-Apr-22 14:34
professionalKevin Marois7-Apr-22 14:34 
Questiontrack active split view Pin
Super Lloyd5-Apr-22 13:47
MemberSuper Lloyd5-Apr-22 13:47 
QuestionViewModel Locator Pin
Kevin Marois5-Apr-22 7:48
professionalKevin Marois5-Apr-22 7:48 
As part of my own framework I am developing Service and View Model Locators. Both are done, but I have one problem.

Here's what I have so far:
Interface
public interface IViewModelLocator
{
    object Get<T>();

    void Register(Type viewType, object viewModel);
}
View Model Locator Class
public class ViewModelLocator : IViewModelLocator
{
    // Internal list of View Models
    private IDictionary<Type, object> _storage;

    public ViewModelLocator()
    {
        _storage = new Dictionary<Type, object>();
    }

    public object Get<T>()
    {
        if (_storage.ContainsKey(typeof(T)))
        {
            return _storage.FirstOrDefault(x => x.Key == typeof(T)).Value;
        }
        else
        {
            throw new Exception($"The requested view '{typeof(T)}' is not registered");
        }
    }

    public void Register(Type viewType, object viewModel)
    {
        if (!_storage.ContainsKey(viewType))
        {
            _storage.Add(viewType, viewModel);
        }
        else
        {
            throw new Exception($"An instance of the view model '{viewModel}' is already registered for the view '{viewType}'");
        }
    }
}
App Setup
public partial class App : Application
{
    public static IServiceLocator ServiceLocator { get; private set; }
    public static IViewModelLocator ViewModelLocator { get; private set; }

    public App()
    {
        ServiceLocator = new ServiceLocator();
        ServiceLocator.Register(typeof(IMyService), new MyService());

        ViewModelLocator = new ViewModelLocator();
        ViewModelLocator.Register(typeof(MainWindowView), new MainWindowViewModel(ServiceLocator.Get<IMyService>()));
        ViewModelLocator.Register(typeof(LoginView), new LoginViewModel());
    }
}
Window Code Behind
public partial class MainWindowView : Window
{
    public MainWindowView()
    {
        InitializeComponent();

        this.DataContext = App.ViewModelLocator.Get<MainWindowView>();
    }
}
Problem
Everything to this point works fine, except that I don't want to have to call the VM locator in code in the code behind. I would like to use it in XAML. I have seen other approaches where they do this:
x:Class="My.MainView"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   DataContext="{Binding Source={StaticResource MyViewModelLocator}, Path=Main}">
There are 2 problems here.
  1. It required a resources entry in App.XAML:
    <Application.Resources>
    <core:ViewModelLocator x:Key="MyViewModelLocator" />
    </Application.Resources>
  2. It requires a property on the VM Locator called "Main". Since these classes are going to be in my framework, there won't be a property for each ViewModel that other apps use on the VM Locator.
What I'd Like
It would be nice if I could do something like:
DataContext="{Binding Path="{x:static TestApp:App.ViewModelLocator.Get<???????>()}"
where the question marks represent the type of the window it's in.

Is there any way to this in XAML?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 5-Apr-22 14:06pm.

AnswerRe: ViewModel Locator Pin
Super Lloyd5-Apr-22 13:56
MemberSuper Lloyd5-Apr-22 13:56 
SuggestionRe: ViewModel Locator Pin
Richard Deeming5-Apr-22 21:27
mveRichard Deeming5-Apr-22 21:27 
AnswerRe: ViewModel Locator Pin
Richard Deeming5-Apr-22 21:42
mveRichard Deeming5-Apr-22 21:42 
GeneralRe: ViewModel Locator Pin
Kevin Marois6-Apr-22 6:11
professionalKevin Marois6-Apr-22 6:11 
QuestionIs it possible to share SQL Server database between WPF application and Ionic application Pin
Alex Dunlop28-Mar-22 7:03
MemberAlex Dunlop28-Mar-22 7:03 
AnswerRe: Is it possible to share SQL Server database between WPF application and Ionic application Pin
Dave Kreskowiak28-Mar-22 10:11
mveDave Kreskowiak28-Mar-22 10:11 
AnswerRe: Is it possible to share SQL Server database between WPF application and Ionic application Pin
Mycroft Holmes28-Mar-22 12:28
professionalMycroft Holmes28-Mar-22 12: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.