我遇到了xaml问题...我创建的按钮未启用。这里是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();
在构造函数,我有:
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 ...}"
)时,它会再次启用!
所以'Command'你绑定必须返回'CanExecute = FALSE'。你可以发布显示'DrawingCanvas.DrawShape'的代码部分吗? – McGarnagle
如何定义'DrawingCanvas.DrawShape'? –
** DrawingCanvas **是一个类,在这个类中** DrawShape **被定义为'public static RoutedCommand DrawShape = new RoutedCommand();' –