我遇到了 xaml 问题...我创建的按钮未启用。这是 xaml 部分:
<Button Margin="0,2,2,2" Width="70" Content="Line"
Command="{x:Static local:DrawingCanvas.DrawShape}"
CommandTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}}, Path=DrawingTarget}"
CommandParameter="Line">
</Button>
在构造函数之前:
public static RoutedCommand DrawShape = new RoutedCommand();
在 ctor 我有:
this.CommandBindings.Add(new CommandBinding(DrawingCanvas.DrawShape, DrawShape_Executed, DrawShapeCanExecute));
然后我有:
private void DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true; **//Isn't this enough to make it enable?**
en.Handled = true;
}
private void DrawShape_Executed(object sender, ExecutedRoutedEventArgs e)
{
switch (e.Parameter.ToString())
{
case "Line":
//some code here (incomplete yet)
break;
}
当我删除块中的第一行(Command="{x:Static ...}"
)时,它会再次启用!