Binding properties between viewmodels - c#

Lets say I have two views: FileList and Editor.
They both have their own view models, bound using a DependencyProperty.
The view models look somewhat like this:
public class FileVM : INotifyPropertyChanged
{
public string FileName { get; set; }
}
public class FileListVM : INotifyPropertyChanged
{
public ObservableCollection<FileVM> Files { get; set; }
public FileVM SelectedFile { get; set; }
}
public public class EditorVM : INotifyPropertyChanged
{
string FileName { get; set; }
}
Imagine that they properly implement INotifyPropertyChanged.
Now I don't want FileListVM and EditorVM to know about each other. They could just as easily exist independently. But in one use case I would like to bind FileListVM.SelectedFile.FileName to EditorVM.FileName. Whenever the selected file or the filename changes, so should the filename in the editor. And whenever the editor changes the filename in the editor, so should the name of the selected file.
Now here is my question: how can I do this cleanly? I'm guessing the solution lies somewhere in their parent view model (e.g. MainVM), since that view model is aware of the connection between the two child view models. But I'm not sure if I can connect the two through xaml, and I'd like to avoid writing property changed event handlers if possible.

Please look at:
Communicate between View Models in MVVM
I advise to use MVVM frameworks like Galasoft because they have useful functionality and often do life with MVVM easier ;)
If you do not like using them, you can add event SelectedFileChanged in one VM and subscribe it from the other one.

Related

Xamarin Forms MVVM with an actual model

I'm fairly new to Xamarin and stumbled across MVVM and really like it as an architectural pattern. However, I found that most HowTo's and tutorials out there only address the VVM (i.e. View-ViewModel) side of things, probably for simplicity sake!?
I would like to know how the communication between a ModelView and its associated models takes place using the INotifyPropertyChanged paradigm and other things.
If I understand correctly, I personally would put stuff like data handling, data storage (collections), db connections and stuff like that into a model. At least this is how I would've been doing it in the good old MVC days. Following questions arouse in my mind:
Where do I create the model(s) and how do I assign them to ViewModels?
How do I properly connect Model and ViewModel such that property updates are propagated and can be handled correctly?
Would you set the model as a member of the ViewModel?
In my current example, I would like to implement a SensorModel which provides several sensory data which layers above can subscribe to. I would like to send updates whenever new sensor data is available to the layers above; i.e. a ViewModel, for instance.
I'd basically had something like this in mind:
class Sensor
{
int _id { get; set; }
string _name { get; set; }
}
class SensorModel
{
private List<Sensor> _sensors { get; set; }
public void addSensor(Sensor s) ...
public void removeSensor(Sensor s) ...
}
Does anybody have links to actual/complete MVVM examples, including the connection between Model and ViewModel?
Any help appreciated.
Use Lastest stable Xamarin Forms
MODELS
In the Project, create a Models folder
To store data, i usually use SQLite or a temp store:
class DataStore
{
public static List<SensorModel> SensorStore { get; set; }
}
Create the SensorModel model
class SensorModel
{
internal int Id { get; set; }
internal string Sensor { get; set; }
}
VIEWMODELS
In the Project, create a ViewModels folder
Create a SensorVM viewmodel
class SensorVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public System.Windows.Input.ICommand StartCommand { get; set; }
public string SensorName { get; set; }
public SensorVM()
{
DataStore.SensorStore = new List<SensorModel>();
StartCommand = new Xamarin.Forms.Command(StartSubmit);
}
private void StartSubmit(object paramter)
{
var sensor = new SensorModel()
{
Id = 1,
Sensor = SensorName
};
AddSensor(sensor);
}
public void AddSensor(SensorModel sensor)
{
//do something
DataStore.SensorStore.Add(sensor);
}
}
VIEWS
In the Project, create a Views folder
Create a Sensor.xaml view
<ContentPage.Content>
<StackLayout Spacing="10" Orientation="Vertical">
<Entry Text="{Binding SensorName}" />
<Button Command="{Binding StartCommand}" Text="Start" />
</StackLayout>
</ContentPage.Content>
In the code behind:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Sensor : ContentPage
{
SensorVM vm;
public Sensor()
{
InitializeComponent();
BindingContext = vm = new SensorVM();
}
}
Hope that helps.
I would like to know how the communication between a ModelView and its
associated models takes place using the INotifyPropertyChanged
paradigm and other things.
I think the best way to create a communication in MVVM is Messaging Center.
https://learn.microsoft.com/pt-br/xamarin/xamarin-forms/app-fundamentals/messaging-center
It's not coupled from device (sensor) code to view models ...
Your messages, in this model, active events that could acess your viewmodels as well as other structures.
A sample of this
In your view use :
public void MessegingCenterInit()
{
#region Bluetooth
MessagingCenter.Subscribe<string, string>("App", "Status_name", (sender, arg) =>
{
App.PVM.Name = $"{arg}";//using INotifyPropertyChanged and view model
viewmodelMethod();//using only a viewmodel
});
#endregion
}
in your model use:
public string Name
{
get { return name; }
set
{
name = value;
App.PVM.Add_patient.AddCanExecuteChanged();//PVM is a viewmodel
//The view model need to have INotifyPropertyChanged as a interface
}
}
In specific code you have (into a generic method or event):
string new_name = John;
MessagingCenter.Send<string,string>("App","Status_name",new_name);
There are several ways to do it, its a simple one, you can try use objects as sender with less information.
Regards
Xamarin itself gives a really good example with their default Master-Detail Solution.
Just create a new Xamarin.Forms App and select the Master-Detail Layout.
It includes several Views, ViewModels (with the BaseVIewModel) and some MockUp Data Classes.
For a start just have a look around there :)
In almost all cases there is no communication between the Model and ViewModel, and very rarely there is communication between the Model and View. If you need to communicate between Model and ViewModel it is extremely likely that you are doing something wrong.
To explain, your model usually describes some entity, like that you have the class Cat:
public class Cat
{
public string Color {get; set;}
}
It is generally used in ViewModel either as the field or as a Collection like:
public class CatsViewModel
{
public List<Cat> Cats {get; set;}
}
The cat shouldn't be able to update by itself, if it is updated it is done either by bindings with the view or somewhere from ViewModel.
So you have some architectural problems in your app, I think.

Using property name and values from main view model in nested view model

I have a view model like below.
I would like to use the property name/values - AllowOrgs and IsOrgOnly in the nested view model - EditAddressViewModel too.
How to accomplish this objective?
My main view looks like this -
#Html.EditorFor(m=> m.AddressEditor,"AddAddress")
My main view model is like this -
public class AddressViewModel
{
public EditAddressViewModel AddressEditor {get; set;}
public bool AllowOrgs { get;set; }
public bool IsOrgOnly { get; set; }
}
Are you asking how you can bind to the AllowOrgs property in AddressEditor?
If so then all you have to do is:
#Html.EditorFor(m=> m.AddressEditor.AllowOrgs,"AddAddress")
And Razor will take care of the rest.
If this not what you meant to ask please provide a little bit more information about what you are looking for.

Catel Mapping properties to Model

I have problem with AutoMapping Catel ViewModel -> Model properties.
I'm using Catel with Fody.Catel
I have a situation when my model changes during runtime.
My Model declaration looks like this:
[Model]
public MyModel SelectedMyModel { get; set; }
And I change this model during runtime like this:
private void TabChangedHandler(TabChangedMessage tabChangedMessage)
{
SelectedMyModel = (MyModel) tabChangedMessage.Data;
}
Then, I'm delegating some properties from Model to ViewModel like this:
[ViewModelToModel("SelectedMyModel")]
public string Name { get; set; }
[ViewModelToModel("SelectedMyModel")]
public string LastName { get; set; }
And the problem is:
When I'm setting this property for the first time, it works nice, but If I change SelectedMyModel to another object, it stops working.
Looks like delegation ViewModel -> Model is no longer working.
Should I call something to let Catel know, that my Model is changing? Or maybe it's a Catel issue?
Can you please help me?

Adding INotifyPropertyChanged to Model?

I'm facing some design questions in my wpf MVVM (Prism based) application, would be happy to get your advice.
My model is very simple:
public class Customer
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
As you can see, I don't have any INotifyPropertyChnaged support for my Model class.
I also have ViewModel for the CustomerDetails screen, that support INotifyPropertyChanged.
public class CustomerDetailsViewModel:INotifyPropertyChanged /*Or NotificationObject*/
{
/*INotifyPropertyChanged event */
private Customer item;
public Customer Item
{
get{return item;}
set
{
item=value;
//Raise PropertyChanged
//Set IsDirty to true
}
}
}
In my view, i'm using binding to the Item.FirstName and my ViewModel being updated.
My problem is - since only the FirstName property is being updated via the View, and the Model itself does not support INotifyPropertyChanged, hence the Item setter not being called, and the IsDirty remains equal to false (and therefore does not update the IsDirty notification on the UI).
I know I can support INotifyPropertyChanged in the model, and then register to the Item.PropertyChanged event in the view model, and actually set the IsDirty to true, But -
Since I'm also using CodeFirst, and my Model class shared between my ServerSide and my client side (Not using Add Service Reference), I don't want to add the INotifyPreoprtyChanged stuff to my server side.
I'm considaring creating new project, that will use T4 templates to copy one by one all my Entities (as Customer) and adding INotifyPropertyChanged support to each and every model.
Is that something that seems reasonable or not? any other suggestions?
Thanks!
Option1.
Separate entities, which being transferred between client and server (DTO), from entities, which are models on the client side. Implement INPC in models. Use mapping between these entities.
Option2.
Bind view to view model properties only. Make view model properties, which wrap corresponding model properties.
Option 3.
Is a mix of first two options. Do not aggregate model in view model. Use mapping between model and view model. Make view model properties, which correspond to model properties.
Well your approach is simply not the best. Much better would be to use a VM like this
public class CustomerDetailsViewModel : INotifyPropertyChanged
{
public CustomerDetailsViewModel(Customer customer)
{
_item = customer;
}
private Customer _item;
public string FirstName
{
get { return _item != null ? _item.FirstName : null; }
set
{
if (_item == null)
_item = new Customer(); // just an example, probably it's not the desired behavior
_item.FirstName = value;
RaisePropertyChanged(...);
}
}
...
}
This would stick to the spirit of MVVM.
If you want your UI to notice when your model property changed, your model class MUST implement INotifyPropertyChanged and similar MVVM interfaces (IDataErrorInfo, etc...) in order to Notify to the UI that the property changed.
That's because you are not always updating your model from the the viewmodel, where you must implement INotifyProperyChanged and notify for changes.
Wrapping corresponding model properties in the viewmodel used when you cannot implement INotifyPropertyChanged in the model class, which makes the viewmodel to grow VERY fast and creates unnecessary code duplication.
Scenario for example:
public class Customer
{
public string FirstName {get;set;}
public string LastName {get;set;}
// Changes the first name.
public void ChangeFirstName(string newName)
{
FirstName = newName;
//The UI will never know that the property changed, and it won't update.
}
}
Solution:
Implement INotifyPropertyChanged in you model class, create backing fields to your properties, and for each property setter, AFTER the set operation, raise OnPropertyChanged invoked method with the property name.
If you don't like to clutter your model with INotifyPropertyChanged code you could try using a NUGet package called PropertyChanged.Fody
You can use it like this;
using PropertyChanged;
[ImplementPropertyChanged]
public class Customer
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
Any public property in this class will now support INotifyPropertyChanged
I think you are on the right track. In the server side, you do not need INotifyPropertyChanged, thus do not add it to the domain classes in the server side.
You may just add some build symbols such as "WPF" to your client projects; and in the code first definitions implement INotifyPropertyChanged only if there is "WPF" build symbol. Then just add your server side domain classes as links to your presentation application. Something like;
#if WPF
public class MyEntity : INotifyPropertyChanged
#else
public class MyEntity
....

What is the best practice for base model in MVC 4 project?

I am starting a new MVC project and it is definetly known that each view required the next items: user language, timezone and few user settings referred to city, country, etc. So, I am going to make some base entity where to put this properties. But some view would be type of PagedList (class from opensource MvcPaging library). And I am not sure how to combine Base model and PagedList in one object?
I thank about interfaces but I read that it is more complex rather then using classes.
sounds like you should look into inheritance:
public class BaseModel
{
public string UserLanguage { get; set; }
...
}
public class SomePageModel : BaseModel
{
public int Id { get; set; }
public PagedList<Whatever> PagedStuff { get; set; }
...
}
In this case SomePageModel inherits all properties of BaseModel. You don't need to use PagedList as a Model. You can use SomePageModel as a model for a view, which has inside the PagedList. In this case you'd write in the view:
#model [namespace].SomePageModel
//to access paged stuff:
#Model.PagedStuff
But really your question lucks specifics and it's hard to direct you in the correct direction based on the information you've provided thus far.

Categories