Hi everyone,
I've been looking up tutorials for the past few weeks (and even more intensely during the past 3 days) about WPF with mvvm approach and have been missing something which I am not sure of. Cutting to the chase, I have created a test application with 4 classes including the MainWindow (during this tutorial I have purpossedly violated the MVVM principle by merging the model and view model classes just so I can browse my code quicker without having too many tabs in visual studio)
so here's my model and view model class which inherits from ObservableObject class
string _partnumber
int _price
Part _currpart
public string PartNumber
{
get {return _partnumber;}
set
{
if (value != _partnumber)
{
_partnumber = value;
OnPropertyChanged("PartNumber");
}
}
}
public int Price
{
set
{
OnPropertyChanged("Price");
}
}
public Part CurrentPart
{
set
{
OnPropertyChanged("Part");
}
}
private ICommand _getpricecommand;
public ICommand GetPriceCommand
{
get
{
if (_getpricecommand == null)
{
_getpricecommand = new RelayCommand(param => GetPrice(),param => true);
}
return _getpricecommand;
}
}
private void GetPrice()
{
Part p = new Part();
p.PartNumber = "6H-4130-01";
p.price = 7;
CurrentPart = p;
}
ObservableObject class
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler hndl = this.PropertyChanged;
if (hndl != null)
{
var e = new PropertyChangedEventArgs(PropertyName);
hndl(this, e);
}
}
public virtual void RaisePropertyChanged(string PropertyName)
{
OnPropertyChanged(PropertyName);
}
and finally, RelayCommand class
readonly Action<object> _execute;
readonly Predicate<object> _canexecute;
public RelayCommand(Action<object> Execute ) : this(Execute,null)
{
}
public RelayCommand(Action<object> Execute, Predicate<object> CanExecute)
{
if(Execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = Execute;
_canexecute = CanExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canexecute == null ? true : _canexecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
for the view, which is the MainWindow, I override the OnStartUp with:
MainWindow app = new MainWindow();
Part p = new Part();
app.DataContext = p;
app.Show();
and finally
<Window.Resources>
<DataTemplate DataType="local:Part" />
</Window.Resources>
<StackPanel DataContext="{Binding CurrentPart}">
<TextBlock Text="{Binding PartNumber}" Height="50" Width="150" />
<TextBlock Text="{Binding Price}" Height="50" Width="150" />
<Button Command="{Binding GetPriceCommand}" Height="50" Width="150" Content="Get Part" />
</StackPanel>
So at the press of Get Part button, it should display the partnumber in the top textbox and price in bottom textbox, but nothing showed up. I placed breakpoints in GetPrice() and GetPriceCommand but they seem to pass on the properties as they should. What did I miss?