I want to create custom property in ViewModel that implements INotifyPropertyChanged and bind that property to datagrid.
So the idea is (for example) i have FirstName and LastName string property in Model, and i want to make property FullName = FirstName + " " + LastName in ViewModel ( i dont want to do it in Model class!), and i want to bind this property in datagrid where i have FirstName, LastName and FullName columns
Here is my model class:
class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//....
}
ViewModel:
class ViewModelUsers : ViewModelBase
{
public ObservableCollection<Models.User> Users { get; private set; }
string _FlllName;
public string FullName
{
//...
}
public ViewModelUsers()
{
//....
}
And my xaml:
<DataGrid ItemsSource="{Binding Users}"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTextColumn Header="First name" Binding="{Binding FirstName, Mode=OneWay}" Width="Auto"></DataGridTextColumn>
<DataGridTextColumn Header="Last name" Binding="{Binding LastName, Mode=OneWay}" Width="Auto"></DataGridTextColumn>
<DataGridTextColumn Header="Full name" Binding="{Binding Path=FullName, Mode=OneWay}" Width="Auto"></DataGridTextColumn>
This code is not complete, there is way more lines, but i hope you understand what i want to achieve. I am new to this c#-WPF (mvvm) so as i understood Model.cs should be "clean", and all logic should be in viewModel. ViewModel inherits ViewModelBase which implements INotifyPropertyChanged.
As i said i am new to this, i am looking for best practice implementing MVVM and learning as i go.