How to get the existing instance of DataContext in xaml ?

maxime Hks 21 Reputation points
2022-03-03T10:42:20.793+00:00

Hi,

Here is my problem

I have a ViewModel A (the vm for my main window) and a ViewModel B(vm for my second window).
And i need to get an object from my VM A to my VM B.

So what i did basicaly is putting parameter in my second window Constructor
then, in the behind code of this window, i manually created an instance of its viewModel, so i could put parameter in it as well.

In a nutshell, from my MainWindowVm

  `SecondWindow second = new SecondWindow (Object objectToBeSent);`

In the behind code of Second window :

`SecondWindowVm vm = new SecondWindowVm(objectToBeSent);`
`DataContext=vm;`

So, in order to make this working, i had to remove from my xaml the DataContext statement
as i always do like :

<Window.DataContext>
        <local:SecondWindowVM/>
    </Window.DataContext>

My bindings still works when i run my program even if Visual Studio tells me DataContext can't be found.
The question is : Is there a way to tell to my xaml that the DataContext is an existing instance of SecondWindowVm called "vm" ?

Thanks

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-03-03T20:33:01.923+00:00

    Hi,
    you can use static class like in this code:

      public class ViewModelA
      {
        public ViewModelA() { if (StaticClass.DataRef == null) StaticClass.DataRef = new Data(); }
        public string Info
        {
          get => StaticClass.DataRef.Info;
          set { StaticClass.DataRef.Info = value; }
        }
      }
      public class ViewModelB
      {
        public ViewModelB() { if (StaticClass.DataRef == null) StaticClass.DataRef = new Data(); }
        public string Info
        {
          get => StaticClass.DataRef.Info;
          set { StaticClass.DataRef.Info = value; }
        }
      }
      public static class StaticClass
      {
        public static Data DataRef { get; set; }
      }
    
      public class Data
      {
        public string Info { get; set; }
      }
    

    in first Window:

    <Window.DataContext>
             <local:ViewModelA/>
    </Window.DataContext>
    

    in second Window:

    <Window.DataContext>
             <local:ViewModelB/>
    </Window.DataContext>
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-03-03T18:44:41.907+00:00

    You need to reference the model in the xaml

    xmlns:classes="clr-namespace:YourNamespace.Models"
    
    1 person found this answer helpful.
    0 comments No comments

  2. maxime Hks 21 Reputation points
    2022-03-07T09:57:36.393+00:00

    Thank you, that's a good idea

    0 comments No comments

  3. Anonymous
    2022-03-07T12:54:01.027+00:00

    Hello,

    Other simple way to directly access VM A in VM B is via application main window instance. use 'System.Windows.Application.Current.MainWindow.DataContext' to refer the view model instance of the VM A anywhere in your application.

    Thanks, Bhushan

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.