Reputation: 590
I am using RelayCommand (called CommandRelay in my code for some reason) as outlined in Josh Smith's old MVVM article. In one of my workspaces I have a textbox that takes any input and, when enter is pressed, the textbox is to be cleared, a message box should appear with "command evoked {0}" where {0} is the text property of the textbox. Additionally this property should also be added to the History string, which is used as the text property of a textblock in the same view. I have managed to get the messagebox working correctly, however, as I am new to WPF and MVVM, I am unsure as to how to correctly add in more tasks to my command.
Here is the Relevant XAML:
<TextBox Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" Name="txtUserEntry">
<TextBox.InputBindings>
<KeyBinding Command="{Binding BindKeyCommand}"
CommandParameter="{Binding ElementName=txtUserEntry, Path=Text}"
Key="Return"
Modifiers=""/>
</TextBox.InputBindings>
</TextBox>
And the relevant command in the ViewModel:
CommandRelay _BindKeyCommand;
public ICommand BindKeyCommand
{
get
{
_BindKeyCommand = new CommandRelay(param => MessageBox.Show(string.Format("Command invoked : {0}", param)));
return this._BindKeyCommand;
}
}
This returns a messagebox with the text successfully.
I have tried several things to get multiple actions out of this command, the most obvious was trying to pass multiple actions to the RelayCommand class, but that also obviously, doesn't work. So then I tried to pass the Param into a seperate Execute function as shown below:
CommandRelay _BindKeyCommand;
public ICommand BindKeyCommand
{
get
{
_BindKeyCommand = new CommandRelay(param => this.ExecuteBindKeyCommand(param));
return this._BindKeyCommand;
}
}
public void ExecuteBindKeyCommand(string param)
{
MessageBox.Show(string.Format("CommandInvoked: {0}", param));
// MORE TASKS HERE
}
This would surely allow me to do multiple things with this command. However I get an error using this method on this line;
_BindKeyCommand = new CommandRelay(param => this.ExecuteBindKeyCommand(param));
The best overloaded method match for 'WPFproject.ViewModels.CLIViewModel.ExecuteBindKeyCommand(string)' has some invalid arguments.
Could someone please help me resolve this problem, and could someone please suggest whether there is a more appropriate way of doing what I wish to achieve (add more tasks to this command).
Upvotes: 0
Views: 1049
Reputation: 9713
CommandParameter
is always an object
, therefore you have to cast it to whatever type you need.
In your case, you can either cast the parameter to a string
, like this:
new CommandRelay(param => this.ExecuteBindKeyCommand((string)param));
Or, it may be a better idea to change your method to accept an object
parameter, then cast it to a string
in the method:
public void ExecuteBindKeyCommand(object param)
{
MessageBox.Show(string.Format("CommandInvoked: {0}", param));
// MORE TASKS HERE
}
That being said, string.Format
accepts objects anyway, so you won't need to worry about that bit.
Upvotes: 1