0

我正在尝试做一些非常简单和基本的事情。如我错了请纠正我:

考虑一个简单的按钮,然后与它关联的方法如下。但是,如果我在 DrawShapeCanExecute() 上更改返回类型,我会得到任何一个(禁用的按钮),那么我会收到一条错误消息:

bool WpfApplication8.DrawingCanvas.DrawShapeCanExecute(object, System.Windows.Input.CanExecuteRoutedEventArgs)' 返回类型错误

public static RoutedCommand DrawShape = new RoutedCommand();


private void DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true; 
} **//Isn't this enough to make it enable?**



private void DrawShape_Executed(object sender, ExecutedRoutedEventArgs e)
{

}

它的 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>
4

3 回答 3

2

您需要将事件标记为已处理:

e.CanExecute = true;
e.Handled = true;
于 2012-10-03T22:19:49.117 回答
0

http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.aspx我看到你需要绑定 CanExecute 和 Executed 处理程序。它会是这样的:

<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:DrawingCanvas.DrawShape }"
                Executed="DrawShape_Executed"
                CanExecute="DrawShapeCanExecute" />
</Window.CommandBindings>

<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>
于 2012-10-03T22:25:11.513 回答
0

在 MainWindow.xaml.cs 中添加这个语句可以解决它:

public IInputElement DrawingTarget { get { return _canvas; } }

关键是我们不是在表单上而是在画布上添加菜单栏。

于 2012-10-04T23:06:54.327 回答