Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between different ViewModels

Tags:

c#

mvvm

wpf

I'm trying to develop an easy MVVM project that it has two windows:

  1. The first window is a text editor, where I bind some properties such as FontSize or BackgroundColor:

    <TextBlock FontSize="{Binding EditorFontSize}"></TextBlock>

its DataContext is MainWindowViewModel:

public class MainWindowViewModel : BindableBase
{     
    public int EditorFontSize
    {
        get { return _editorFontSize; }
        set { SetProperty(ref _editorFontSize, value); }
    } 
.....
  1. The second window is the option window, where I have an slider for changing the font size:

<Slider Maximum="30" Minimum="10" Value="{Binding EditorFontSize }" ></Slider>

its DataContext is OptionViewModel:

public class OptionViewModel: BindableBase
{     
    public int EditorFontSize
    {
        get { return _editorFontSize; }
        set { SetProperty(ref _editorFontSize, value); }
    }
.....

My problem is that I have to get the value of the slider in the option window and then I have to modify the FontSize property of my TextBlock with this value. But I don't know how to send the font size from OptionViewModel to MainViewModel.

I think that I should use:

  1. A shared model
  2. A model in MainWindowViewModel and a ref of this model in OptionViewModel
  3. Other systems like notifications, messages ...

I hope that you can help me. It's my first MVVM project and English isn't my main language :S

Thanks

like image 986
ganchito55 Avatar asked Feb 11 '16 16:02

ganchito55


People also ask

Can fragment have 2 ViewModels?

In fact you can have multiple view models for a single fragments doing different things for you.

How to share one project class with multiple viewmodels?

In fact, the Project class should be a model for your ViewModels. Just pass it in vm's constructor. If you really need to share one instance of Project class in multiple vm's, then use factories and some type of cache when constructing view models.

When to use share a view model in SharePoint?

Sharing a View Model can be useful in the following scenarios: Master/Detail flow. Main View with a Modal (Dialog, BottomSheet, etc). In our case, we have a Master/Detail flow: ShowNewsFragment allows the user to search for news and NewsDetailFragment will show the selected news detail.

How to open two windows on the same view model?

now you have to make another constructor for your OptionWindow passing a view model. public SecondWindow (BindableBase viewModel) { InitializeComponent (); this.DataContext = viewModel; } this is to make sure that both windows work on the same instance of a view model. Now, just wherever you're opening the second window use these two lines

How to communicate between 2 view models using messenger?

We will how we can communicate between 2 View Models using Messenger. Then, we will create 2 MVVM Controls by name ControlA, ControlB and try to call a method in which is there is one view model within another view model. Go to Nuget manager and install MVVM Light. Create a Folder structure like below.


1 Answers

Another option is to store such "shared" variables in a SessionContext-class of some kind:

public interface ISessionContext: INotifyPropertyChanged 
{
    int EditorFontSize { get;set; }
}

Then, inject this into your viewmodels (you are using Dependency Injection, right?) and register to the PropertyChanged event:

public class MainWindowViewModel 
{
    public MainWindowViewModel(ISessionContext sessionContext)
    {
        sessionContext.PropertyChanged += OnSessionContextPropertyChanged;        
    }

    private void OnSessionContextPropertyChanged(object sender, PropertyChangedEventArgs e) 
    {
        if (e.PropertyName == "EditorFontSize")
        {
            this.EditorFontSize = sessionContext.EditorFontSize;
        }
    }       
}
like image 193
RoelF Avatar answered Oct 04 '22 04:10

RoelF