我希望获得NamePlaylist
的{{1}}我点击MenuContext。但是参数总是空的我不知道为什么。
这是ListView中的菜单
XML
Item
一个CommandProvider
<MenuItem Header="Add" ItemsSource="{Binding Path=ItemSourcePlaylist}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding TestCall}" CommandParameter="{Binding NamePlaylist}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Name="NamePlaylistText" Text="{Binding Path=NamePlaylist}"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
拨打
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;
}
功能
public ICommand TestCall { get { return new RelayCommand(obj => this._settingsFunction.TestFunction(obj)); } }
答案 0 :(得分:1)
如果我了解您的视图模型,则ItemSourcePlaylist
和TestCall
位于一个类中,NamePlaylist
是ItemSourcePlaylist
项的属性。如果是这种情况,我建议使用ItemsContainerStyle
并将Command
属性绑定到父级DataContext
和CommandParameter
到当前项DataContext
<MenuItem Header="Add" ItemsSource="{Binding Path=ItemSourcePlaylist}">
<MenuItem.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}}, Path=DataContext.TestCall}"/>
<Setter Property="CommandParameter" Value="{Binding NamePlaylist}"/>
</Style>
</MenuItem.ItemContainerStyle>
<MenuItem.ItemTemplate>
<DataTemplate>
<TextBlock Name="NamePlaylistText" Text="{Binding Path=NamePlaylist}"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>