我已经为我的一个按钮实现了一个自定义 IComand 类。该按钮被放置在页面“MyPage.xaml”中,但其自定义 ICommand 类被放置在另一个类中,而不是在 MyPage 代码后面。然后从 XAML 我想将按钮与它的自定义命令类绑定,然后我这样做:
MyPage.xaml:
<Page ...>
<Page.CommandBindings>
<CommandBinding Command="RemoveAllCommand"
CanExecute="CanExecute"
Executed="Execute" />
</Page.CommandBindings>
<Page.InputBindings>
<MouseBinding Command="RemoveAllCommand" MouseAction="LeftClick" />
</Page.InputBindings>
<...>
<Button x:Name="MyButton" Command="RemoveAllCommand" .../>
<...>
</Page>
和自定义命令按钮类:
// Here I derive from MyPage class because I want to access some objects from
// Execute method
public class RemoveAllCommand : MyPage, ICommand
{
public void Execute(Object parameter)
{
<...>
}
public bool CanExecute(Object parameter)
{
<...>
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
我的问题是如何说 MyPage.xaml 按钮的 Execute 和 CanExecute 方法在另一个类中,而不是按钮所在位置的代码。怎么说这些方法在 XAML 页面的 RemoveAllCommand 类中。
我还想在按钮中产生单击鼠标事件时触发此命令,所以我进行输入绑定,是否正确?
谢谢