Reputation: 1777
I want to implement the CommandParameter in my class CommandProvider, which use for a command (Button, etc.) and inherit from ICommand, but I didn't understand how to implement that. Example:
XAML
<TreeViewItem Header="Playlist" ItemsSource="{Binding ItemSourceTree}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=NewPlaylist}"
CommandParameter="{Binding Path=NamePlaylist}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeViewItem.ItemTemplate>
<DataTemplate DataType="{x:Type local:PlaylistDB}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=NamePlaylist}">
</TextBlock>
</StackPanel>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
The console says, NamePlaylist doesn't found.
And link a function to the Binding NewPlaylist
:
public ICommand NewPlaylist { get { return new CommandProvider((obj) => DoubleClickTest(obj)); } }
Function
public void DoubleClickTest(object obj)
{
var tmp = obj as string;
Console.WriteLine(tmp);
}
So I need to modify my class CommandProvider to take parameter right? How I can do that?
CommandProvider
public class CommandProvider : ICommand
{
#region Constructors
public CommandProvider(Action<object> execute) : this(execute, null) { }
public CommandProvider(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute(parameter) : true;
}
public void Execute(object parameter)
{
if (_execute != null)
_execute(parameter);
}
public void OnCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}
#endregion
private readonly Action<object> _execute = null;
private readonly Predicate<object> _canExecute = null;
}
PlaylistDB
public class PlaylistDB
{
public string NamePlaylist { get; set; }
}
I want to retrieve the NamePlaylist
in my function DoubleClickTest()
, and I want to pass it in CommandParameter
. How can I do that?
Upvotes: 2
Views: 2137
Reputation: 2956
Use The below Class for accepting commandparameters
Using ICommand
,
public class DelegateCommand: ICommand
{
#region Constructors
public DelegateCommand(Action<object> execute)
: this(execute, null) { }
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute(parameter) : true;
}
public void Execute(object parameter)
{
if (_execute != null)
_execute(parameter);
}
public void OnCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}
#endregion
private readonly Action<object> _execute = null;
private readonly Predicate<object> _canExecute = null;
}
Usage:
public ICommand CloseCommand
{
get
{
return new DelegateCommand((obj)=>CloseMethod(obj));
}
}
obj
is the command parameter
passed in the above example.
Upvotes: 4