madaxe2020
Well-known member
- Joined
- Sep 7, 2020
- Messages
- 50
- Programming Experience
- 5-10
I hope somebody can help, I'm learning the M-V-VM pattern and in this example the Button should become deactivated when there is no string in the text field and when the button is selected its suppose to throw a debug assert.
There is no error , warnings it just does not work, the button seems to be detached from the view model and i dont know why
Thanks
Madaxe
There is no error , warnings it just does not work, the button seems to be detached from the view model and i dont know why
Thanks
Madaxe
Model:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace MVVM_Learning.Models
{
public class CustomerModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Name;
public string Name
{
get
{
return this._Name;
}
set
{
this._Name = value;
NotifyPropertyChanged("Name");
}
}
public CustomerModel(string CustomerName)
{
this.Name = CustomerName;
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
View Model:
using MVVM_Learning.Commands;
using MVVM_Learning.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Input;
namespace MVVM_Learning.ViewModels
{
public class CustomerViewModel
{
public ICommand UpdateCommand { get; private set; }
public CustomerModel customerModel {get; private set; }
public bool CanUpdate
{
get
{
if(this.customerModel == null)
{
return false;
}
return !String.IsNullOrWhiteSpace(this.customerModel.Name);
}
}
public CustomerViewModel()
{
// Normally Get From Database
this.customerModel = new CustomerModel("Bob");
this.UpdateCommand = new CustomerUpdateCommand(this);
}
public void SaveChanges()
{
// Normally Save to Database
Debug.Assert(false, String.Format("{0} Was Updated.", this.customerModel.Name));
}
}
}
View:
using MVVM_Learning.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MVVM_Learning.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new CustomerViewModel();
}
}
}
Command:
using MVVM_Learning.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
namespace MVVM_Learning.Commands
{
internal class CustomerUpdateCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
private CustomerViewModel _customerViewModel;
public CustomerUpdateCommand(CustomerViewModel cusomerViewModel)
{
this._customerViewModel = cusomerViewModel;
}
public bool CanExecute(object parameter)
{
return this._customerViewModel.CanUpdate;
}
public void Execute(object parameter)
{
this._customerViewModel.SaveChanges();
}
}
}