Friday, December 4, 2009

Relay Command - How to Pass a Parameter?

Ever wondered what the param in RelayCommad does? May sound silly though but can't find many samples explaining the same.

Before that, if you are not aware what a Relay Command in MVVM is, you may have to check out what MVVM foundation is all about. Relay Command is actually a commanding manager kinda stuff available with MVVM Foundation (search for the same in codeplex, you will come to know if you don't know) which could reduce the redundancy of the number of commands that may get created in a ViewModel's perspective.

A Relay Command implementation in general will look like

RelayCommand _openCommand
public ICommand OpenCommand
{
get
{
if (_openCommand == null)
{
_openCommand = new RelayCommand(param => this.Open,
param => this.CanOpen );
}
return _openCommand;
}
}

So what's the param doing there?

I initially thought it's just for the sake of the Lambda construct that we are having to write it. But then when i really came across instances where i had to pass a parameter to an Action or a Predicate, i knew param had something to say.

Let's say i have a situation where in need to pass a parameter to the Open Action as well as to the CanOpen Predicate.

RelayCommand _openCommand
public ICommand OpenCommand
{
get
{
if (_openCommand == null)
{
_openCommand = new RelayCommand(param => this.Open((string)param),
param => this.CanOpen((string)param) );
}
return _openCommand;
}
}

One of the ways of doing it would be to add a property (CommandParameter or something) in your ViewModel which you can bind with your controls CommandParameter.

public object CommandParameter { get; set; }

To make it more clear, let's think about an implementation where i need to open a file by passing the file path as the parameter. So the CanOpen predicate will function something like if i don't find the file to be opened in the defined path, i won't allow the OpenCommand to execute.
So in my code the implementation will look something like,

viewModel.CommandParameter = strFilePath;

Xaml Binding will have the Command and CommandParameter part related to whatever control we are binding to and the predicate will look something like.

public bool CanOpen(string strFilePath)
{
//if i can't find the file, i should not try to open it.
return File.Exists(strFilePath);
}

and similarly the Action part implementation
public void Open(string strFilePath)
{
//Open the file and i do whatever i want to do....
}

pretty neat in an environment where the path may be read dynamically from somewhere.

Tuesday, December 1, 2009

WPF Apps....Click Once Deploy...

Check out this Vertigo site where you will get a wealth of WPF and Silverlight samples.

I found the Family Show sample really interesting with some really good animation....

Friday, September 18, 2009

Data Driven Display - Dynamic Loading

WPF comes with a clean approach when it comes to loading user controls dynamically. A Windows Application developer always had the vision for a "Data driven Model" where the data can decide what it needs to display to the user but he never had the platform to deliver it. With the advent of WPF, he can actually make the data render the User Interface the way it needs it to be like. This is happening because of one magic called DataTemplate whose use we have already seen in my earlier posts in TreeView implementation etc.

Let me explain it to you a little bit in more detail.

Suppose we have defined a Base Type called Animal and we inherit the Type to define a Cat, a Dog and a Man. Now i add all these Animal objects created to a ListView and i want the corresponding types added to the ListView to display the objects as Images of itself. So what we should do is to define the DataTemplate as show below.

<DataTemplate DataType={x:Type Cat}>
<Image Source=CatImage.jpg/>
</DataTemplate>

<DataTemplate DataType={x:Type Dog}>
<Image Source=DogImage.jpg/>
</DataTemplate>

<DataTemplate DataType={x:Type Man}>
<Image Source=ManImage.jpg/>
</DataTemplate>

The above piece of code is just a pointer as to what needs to be done to make the implementation work like data driven.

Monday, August 3, 2009

Wpf is not Killing Winforms

The outcome of a poll that happened in my blog site which asked the readers "Is Wpf gonna kill Winforms?" is a clear and unanimous "NO". That's good news for Winforms enthusiasts.