Answer the question
In order to leave comments, you need to log in
How to write in MVVM?
Please tell me how to properly use the MVVM pattern in a WPF application. Example classes:
Using a control in a third-party class (Creating from code):
BrowserView someBrowser = new BrowserView();
container.Children.Add(someBrowser);
someBrowser.Navigate("http://google.ru/");
someBrowser.SomeControlText = "Перешёл на сайт google.ru";
public partial class BrowserView : UserControl
{
BrowserViewModel viewModel;
public BrowserView()
{
InitializeComponent();
viewModel = new BrowserViewModel(this.GetType());
this.DataContext = viewModel;
}
public string SomeControlText
{
get { return viewModel.Model.SomeControlText; }
set { viewModel.Model.SomeControlText = value; }
}
public void Navigate(string newAddress)
{
viewModel.Model.SomeBrowser.Address = newAddress;
}
}
<UserControl x:Class="GPClient.View.BrowserView">
<Grid x:Name="BrowserContainer">
<ContentControl Content="{Binding Path=Model.SomeBrowser}"></ContentControl>
</Grid>
<SomeControl Text="{Binding Path=Model.SomeControlText}" />
</UserControl>
public class BrowserViewModel : Notifier
{
public BrowserModel Model { get; private set; }
public BrowserViewModel(Type typeControl)
{
Model = new BrowserModel();
ChromiumWebBrowser browser = new ChromiumWebBrowser();
Model.SomeBrowser = browser;
}
}
public class BrowserModel : Notifier
{
ChromiumWebBrowser someBrowser;
public ChromiumWebBrowser SomeBrowser
{
get { return someBrowser; }
set { someBrowser = value; OnPropertyChanged("SomeBrowser"); }
}
string someControlText;
public string SomeControlText
{
get { return someControlText; }
set { someControlText = value; OnPropertyChanged("SomeControlText"); }
}
}
someBrowser.viewModel.Model.SomeControlText = "какой-то текст";
someBrowser.viewModel.SomeControlText = "какой-то текст";
public string SomeControlText
{
get { return viewModel.Model.SomeControlText; }
set { viewModel.Model.SomeControlText = value; }
}
someBrowser.SomeControlText = "какой-то текст";
Answer the question
In order to leave comments, you need to log in
To get started, take some kind of MVVM framework (SimpleMVVM, MVVMLight, they have a bunch of examples), make your own view, ViewModel. In these frameworks, the base classes for creating a VM are already implemented (INotyfyPropertyChanged is implemented). In the VM, you need to add the necessary properties with which the properties of the elements will be bound. It is also desirable to make a service class that will create a model on demand, register an instance of the class in App.Resources, and bind the necessary VM immediately in Xalm, to DataContext="{Binding GroupsViewModel, Mode=OneWay, Source={StaticResource Locator}} "> (the locator has a GroupsViewModel property, in which the necessary VM is created, all parameters are passed to services (for example, working with a database).
Creating and passing elements, or working with controls directly, increases code cohesion, and so on.
those for example in your case:
locator.cs
public class Locator
{
public MainViewModel MainVM
{
get {return new MainViewModel(); }
}
}
public class MainViewModel : Notifier
{
MainViewModel ()
{
}
string address;
public string Address;
{
get { return address; }
set { address = value; OnPropertyChanged("Address"); }
}
string someControlText;
public string SomeControlText
{
get { return someControlText; }
set { someControlText = value; OnPropertyChanged("SomeControlText"); }
}
}
<UserControl x:Class="GPClient.View.BrowserView" DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">>
<ChromiumWebBrowser Address="{Binding Path=Address, Mode=TwoWay}"/>
<SomeControl Text="{Binding Path=SomeControlText, Mode=TwoWay}" />
</UserControl>
If relevant, then I can show an example with the MvvmLight library.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question