1) What are the best ways to pass data between multiple Views?
2) I have scenario(MVVM C#):
TextBox and Button in MainWindow and TextBlock in Window1, on Button click(I am using Icommand) the Data in TextBox of MainWindow has to appear in TextBlock of Window1 ?
ViewModelBase.cs
public class ViewModelBase
{
public Commandclass commandclass { get; set; }
public ViewModelBase()
{
commandclass = new Commandclass(this);
}
private string fname;
public string vmname
{
get { return fname; }
set { fname = value; }
}
public void OnCommand()
{
Window1 w = new Window1();
/* How to bind ???*/
w.Show();
}
}
CommandClass.cs
public class Commandclass : ICommand
{
public ViewModelBase vmclass { get; set; }
public Commandclass(ViewModelBase vmb)
{
vmclass = vmb;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
vmclass.OnCommand();
}
}
Views
**MainWindow.xaml**
<Window x:Class="Multiwindow.MainWindow"
…
xmlns:vm="clr-namespace:Multiwindow.Viewmodel">
<Window.Resources>
<vm:ViewModelBase x:Key="vmodel"/>
</Window.Resources>
<Grid Background="Gray" DataContext="{StaticResource vmodel}">
<TextBox Height="26" Margin="194,115,154,179" Width="169"
Text="{Binding vmname, Mode=TwoWay}"/>
<Button Content="Button1" HorizontalAlignment="Left"
Margin="251,158,0,0" VerticalAlignment="Top"
Command="{Binding commandclass, Source={StaticResource vmodel}}"/>
</Grid>
</Window>
**Window1.xaml**
<Window.Resources>
<vm:ViewModelBase x:Key="vmodel"/>
</Window.Resources>
<Grid >
<TextBlock FontSize="20" Height="28" Width="169" Foreground="Black"
Background="Bisque" />
</Grid>
I have googled and found one project but it is to complex, Please suggest an answer to my 2) question will be helpfull.. Thanks.
MVVM CA is one of the most powerful architecture that produces a well thought separation of responsibilities. It uses Coordinator pattern with Assembler and Repository pattern which makes View Controllers decoupled and view models light ensuring all the Design Principles. It is also a variant of MVVM-C architecture.
Coordinator pattern provides an encapsulations of navigation logic. In other words : Instead of pushing and presenting your ViewControllers from other view controllers. All the screens navigation will be managed by coordinatos.
Despite its name, the MVVM pattern includes four major components, model, view, view model, and controller. The implementation of a view model is usually straightforward. All it does is translate data from the model to values the view(s) can display. The controller is no longer responsible for this ungrateful task.
This is how i would do this. In the command called on button click I'd do this:
Window2 w= new Window2();
w.DataContext=new Window2ViewModel();
((Window2ViewModel)w.DataContext).TextForTextblock=TextFromTextbox;
w.Show();
Edit
Seeing your code, You can do this as I think both windows share ViewModelBase:
Window1 w= new Window1();
w.DataContext=this;
w.Show();
You also have to bind your TextBlock:
<TextBlock FontSize="20" Height="28" Width="169" Foreground="Black"
Background="Bisque" Text="{Binding vmname}"/>
I assume that you intended to share ViewModelBase
between MainWindow and Window1. In that case you can't add ViewModelBase instance to MainWindow.Resources and then another instance to Window1.Resources. If you don't unserstand why, please check some C# OOP tutorial.
To share the same instance of ViewModelBase between multiple view you must create only one resource. Application.Resources are accessible from all view.
that's it.
However, it is recommended, that you have separate ViewModel classes for each View.
In that case you could have somthing like this:
<Window x:Class="Multiwindow.MainWindow"
xmlns:vm="clr-namespace:Multiwindow.Viewmodel">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
...
</Window>
**Window1.xaml**
<Window.DataContext>
<vm:Window1ViewModel />
</Window.Resources>
In addition to @Pikoh's solution I propose following:
Constructor parameter:
var window1 = new Window1("hello world");
windows1.Show();
public class Window1(string parameter){...
I preffer ViewModel property on the View and let the reposibility for creating ViemModel on the View.
var window1 = new Window1();
window1.ViewModel.Parameter = "Hello world";
public class Window1{
...
public Window1ViewModel { get {return (Window1ViewModel)DataContext;}}
}
datacontext should be set in ctor or in XAML.
ViewModel first approach:
a) create viewmodel for second window
b) set the parameters on the viewmodel
c) use custom DialogService
class to create the view for you based on the viewmodel type using naming convention and show it.
this way you dont even touch any view in your viewmodels and you have your viewmodel trully separated from your views, so its easily testable. You can easily replace DialogService implementation when running unit tests. .
//in MainWindowViewModel:
var secondWindowViewModel = new SecondWindowViewModel();
//alternativelly:
//secondWindowViewModel = ViewModelLocator.Resolve<SecondWindowViewModel>();
secondWindowViewModel.Parameter = "Hello world";
dialogService.Show(secondWindowViewModel);
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