2012-10-03 46 views
0

我正在嘗試做一些非常簡單和基本的事情。糾正我,如果我錯了:WPF中的禁用按鈕

想想一個簡單的按鈕,然後關聯它的方法如下。但是,我越來越如果我改變對DrawShapeCanExecute返回類型要麼沒有(被禁用按鈕)(),那麼我會得到一個錯誤消息:

布爾WpfApplication8.DrawingCanvas.DrawShapeCanExecute(對象,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> 

回答

0

添加在MainWindow.xaml.cs這一說法解決它:

public IInputElement DrawingTarget { get { return _canvas; } }

的一點是,我們還添加了菜單欄不是形式,而是在畫布上。

2

你需要爲處理標記事件:

e.CanExecute = true; 
e.Handled = true; 
+0

如果你的意思是同時使用e.CanExecute = true和e.Handled = ture ...是的,但它是不行的。 –

+0

@amitkohan,你是否嘗試在事件處理程序中放置一個斷點來檢查它是否被調用? –

+0

好問題!是的,我在'this.CommandBindings.Add(新的CommandBinding(DrawingCanvas.DrawShape,DrawShape_Executed,DrawShapeCanExecute))上放置了一個斷點;'這裏發生的事情是,當我將鼠標懸停在** DrawShapeCanExecute **上時,它的提示顯示了這條消息: DrawShapeCanExecute(...)具有錯誤的返回類型 –

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> 
+0

我不確定,因爲我已經在''塊中定義了它,而不是在普通的''塊中。文件本身位於項目**/Resources/Views/AppToolbar.xaml **中的文件夾下,並且它是ResourceDictionary –