Binding variables as command parameter through binding expression
Is there any way to bind local varaibles and objects to command as command parameter. If any one of the above is possible then please tel开发者_StackOverflow社区l me.
If you mean binding to a 'local variables', then its clearly not possible. You are setting up DataContext
to some object, and then you can only bind to its properties or dependencyproperties, not local variables of some methods, which doesn't sound logical.
You need to be more specific. Can you post some code?
You can do something like:
ICommand command = new ActionCommand(parameter => { this.CallFunction(parameter); });
Parameter is a type of object so you can pass any single object and then unbox it. Also ActionCommand requires Blend or at least the Microsoft.Expression.Interactions assembly.
UPDATED
Ok in this case you are probaly best to define the ICommand on the view model and bind to it in XAML.
On the view model add an implementation like this:
public class AViewModel
{
private ICommand _ACommand;
public ICommand ACommand
{
get
{
if (this._ACommand == null)
{
this._ACommand = new ActionCommand(parameter =>
{
// do stuff.
});
}
return(this._ACommand);
}
}
}
In XAML you need to bind to the data source which you have probaly already done.
<UserControl.Resources>
<local:AViewModel x:Key="AViewModelDataSource" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource AViewModelDataSource}}">
<TextBox x:Name="ABCTextBox" />
<Button x:Name="AButton" Command="{Binding ACommand, Mode=OneWay}" CommandParameter="{Binding ElementName=ABCTextBox, Path=Text}" />
</Grid>
Hope this helps.
精彩评论