Ok. I'm following a tutorial to master MVVM Pattern to my app.
I've followed the tutorial quite well, I got to the part of commandings and when I try to apply command to a button it doesnt seem to work at all, my button never disables. I've seen other MVVM sources and my code seems to be fine besides I found this tutorial on internet it was meant for WPF, but it also apply for UWP Windows 10 Apps.
So I'll paste my classes and Xaml views.
this is my Model:
using System.ComponentModel;
namespace MVVMDemo.Model
{
public class Student : INotifyPropertyChanged
{
private string firstName;
private string lastName;
public string FirstName
{
get
{
return firstName;
}
set
{
if (firstName != value)
{
firstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
}
public string LastName
{
get { return lastName; }
set
{
if (lastName != value)
{
lastName = value;
RaisePropertyChanged("LastName");
RaisePropertyChanged("FullName");
}
}
}
public string FullName
{
get
{
return firstName + " " + lastName;
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}
The model code I believe has no errors on it. this is my ViewModel:
using MVVMDemo.Model;
using System.Collections.ObjectModel;
namespace MVVMDemo.ViewModel
{
public class StudentViewModel
{
/*Let’s add a property of MyICommand type in StudentView Model class.
* Now we need to construct an instance in the StudentViewModel. We will
* use the overloaded constructor of MyICommand that takes two parameters.*/
public MyICommand DeleteCommand { get; set; }
/*Now add the implementation of OnDelete and CanDelete methods.*/
public StudentViewModel()
{
LoadStudents();
DeleteCommand = new MyICommand(OnDelete, CanDelete);
}
private bool CanDelete()
{
//if (SelectedStudent != null)
// return true;
//return false;
return SelectedStudent != null;
}
private void OnDelete()
{
Students.Remove(SelectedStudent);
}
/*We also need to add a new SelectedStudent so that the user
* can delete the Selected Item from ListBox.*/
private Student _selectedStudent;
public Student SelectedStudent
{
get
{
return _selectedStudent;
}
set
{
_selectedStudent = value;
DeleteCommand.RaiseCanExecuteChanged();
}
}
public ObservableCollection<Student> Students
{
get;
set;
}
public void LoadStudents()
{
ObservableCollection<Student> students = new ObservableCollection<Student>();
students.Add(new Student { FirstName = "Mark", LastName = "Allain" });
students.Add(new Student { FirstName = "Allen", LastName = "Brown" });
students.Add(new Student { FirstName = "Linda", LastName = "Hamerski" });
Students = students;
}
}
}
This is my ViewModelLocator:
using MVVMDemo.ViewModel;
namespace MVVMDemo.VML
{
public class ViewModelLocator
{
private static StudentViewModel studentViewModel = new StudentViewModel();
public static StudentViewModel StudentViewModel
{
get { return studentViewModel; }
}
}
}
and at Last this is my View my UserControl StudentView
<UserControl
x:Class="MVVMDemo.Views.StudentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MVVMDemo.Views"
xmlns:vml="using:MVVMDemo.VML"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=StudentViewModel}"
d:DesignHeight="300"
d:DesignWidth="400">
<!--We will apply MVVM Using ItemTemplates-->
<UserControl.Resources>
<DataTemplate x:Key = "studentsTemplate">
<StackPanel Orientation = "Horizontal">
<TextBox Text = "{Binding Path = FirstName, Mode = TwoWay}"
Width = "100" Margin = "3 5 3 5"/>
<TextBox Text = "{Binding Path = LastName, Mode = TwoWay}"
Width = "100" Margin = "0 5 3 5"/>
<TextBlock Text = "{Binding Path = FullName, Mode = OneWay}"
Margin = "0 5 3 5"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<!--In StudentView.xaml, we need to add SelectedItem property in a ListBox
which will bind to the SelectStudent property.-->
<ListView ItemsSource="{Binding Students}"
ItemTemplate="{StaticResource studentsTemplate}"
SelectedItem="{Binding SelectedStudent}"/>
<Button Content = "Delete"
Command="{Binding DeleteCommand}"
HorizontalAlignment = "Left"
VerticalAlignment = "Top"
Width = "75"/>
</StackPanel>
</Grid>
</UserControl>
I'm applying the viewmodellocator on the app.xaml file at application level:
<Application.Resources>
<vml:ViewModelLocator x:Key="mainViewModelLocator"/>
</Application.Resources>
and I call my view on the main page like this:
can somebody help me, I've run this code on VS2015 and VS2017 and the Delete button never gets enabled and it also doesnt delete the rows from the listview or listbox i've tried changing them and nothing seems to be different.
I've even had a commanding implemetation without using the relaycommand pattern as it is used here. I would appreciate your help.
Thanks!!!