I'm trying to develop an easy MVVM project that it has two windows:
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); }
}
.....
<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:
I hope that you can help me. It's my first MVVM project and English isn't my main language :S
Thanks
In fact you can have multiple view models for a single fragments doing different things for you.
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.
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.
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
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.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With