我遇到了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();' –