您好,我正在尝试将命令绑定到 LongListSelector 的 ItemTemplate 中的按钮。但面临绑定问题。这是我的代码-
XAML
<DataTemplate x:Key="ItemTemplate">
<StackPanel Height="108" Width="308" Margin="6,6">
<TextBlock Text="{Binding Name}" Foreground="red"></TextBlock>
<TextBlock Text="{Binding Type}" Foreground="red"></TextBlock>
<Button Content="add to emergency" Foreground="White" Background="red" Command="{Binding Path=DataContext.MyViewModelCommand}"/>
</StackPanel>
</DataTemplate>
命令
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
视图模型
public class HspitalVM:INotifyPropertyChanged
{
public HspitalVM()
{
MyViewModelCommand = new ActionCommand(DoSomething);
}
public ICommand MyViewModelCommand { get; private set; }
private void DoSomething()
{
}
}
该命令适用于裸按钮,但不适用于 ItemTemplate。请指导我