我想在一个单独的类中实现ICommand,并希望从这里访问该按钮:
<Grid>
<Button Command="{StaticResource Do}" >Do!</Button>
</Grid>
public class DoCommand : ICommand
{
public void Execute(object parameter)
{
**I need to access a xaml that triggered this command from here**
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
我怎么能这样做? 感谢。
答案 0 :(得分:1)
您可以将按钮作为参数从XAML传递,如下所示:
<Button Command="{StaticResource Do}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, BindsDirectlyToSource=True}" >Do!</Button>
然后在Execute方法中:
public void Execute(object parameter) { 按钮=参数为按钮; }