I am having a problem with xaml … a button I have created is not enable. here is the xaml part:
<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>
Before Constructor it goes:
public static RoutedCommand DrawShape = new RoutedCommand();
in ctor I have:
this.CommandBindings.Add(new CommandBinding(DrawingCanvas.DrawShape, DrawShape_Executed, DrawShapeCanExecute));
Then I have:
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;
}
When I remove the first line (Command="{x:Static ...}") in the block it gets enable again!
Be sure the
CanExecuteproperty of that command is returning true. If it returns false, it automatically disables the control that utilizes that command.Can execute should return a bool, I’m a little surprised that doesn’t give a compile error. Anyway try to change it to this.
EDIT:
Ok since you just revealed all you want is a simple button that executes a command here’s a very simple implementation copied from one of my recent projects. First define this class somewhere.
Next define a command in your view model and define both the properties I created in the generic command (it’s just the basic stuff that comes along with implementing the ICommand interface).
Then just wire up the button to your command.
If this is all too much for you (MVVM does require a little extra initial setup). You can always just do this…