tags:

views:

468

answers:

2
<Window x:Class="WorkOut.ToggleButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:WorkOut.Code"        
Title="ToggleButton" Height="300" Width="300">
<Grid>
    <StackPanel Orientation="Vertical">
        <ToolBar Height="40" VerticalAlignment="Top">
            <Button Margin="0,3,0,3" Padding="2" HorizontalContentAlignment="Left"
                    Command="{x:Static s:MyCanvas.AddNewTab}"
                    CommandTarget="{Binding ElementName=MyCanvas}">
                <Button.Content>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="3" Text="Append New Tab" VerticalAlignment="Center" Grid.Column="1"/>
                    </Grid>
                </Button.Content>
            </Button>
        </ToolBar>
        <Grid x:Name="MyGrid">
        </Grid>               
    </StackPanel>
</Grid>

public ToggleButton()
    {
        InitializeComponent();

        MyCanvas MyCanvas1 = new MyCanvas();
        MyCanvas1.Name = "MyCanvas";
        MyCanvas1.Background = System.Windows.Media.Brushes.LightBlue;
        MyCanvas1.Height = 100;
        MyCanvas1.Width = 100;
        MyCanvas1.HorizontalAlignment = HorizontalAlignment.Left;
        MyGrid.Children.Add(MyCanvas1);

        MyCanvas MyCanvas2 = new MyCanvas();
        MyCanvas2.Name = "MyCanvas";
        MyCanvas2.Background = System.Windows.Media.Brushes.Beige;
        MyCanvas2.Height = 100;
        MyCanvas2.Width = 100;
        MyCanvas2.HorizontalAlignment = HorizontalAlignment.Right;
        MyGrid.Children.Add(MyCanvas2);
    }
class MyCanvas : Canvas
{
    public static RoutedCommand AddNewTab = new RoutedCommand();
    public MyCanvas()
    {
        this.CommandBindings.Add(new CommandBinding(MyCanvas.AddNewTab, AddNewTab_Executed, AddNewTab_Enabled));
    }
    private void AddNewTab_Executed(object sender, ExecutedRoutedEventArgs e)
    {
         MessageBox.Show (this.Background.ToString());
    }
    private void AddNewTab_Enabled(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}

The above code create two canvas on grid control and the addnew button in tool bar disabled, eventhough it is bound to MyCanvas element.

may be i am following a wrong approach...

any help, much appreciated.

Thanks KJ

+1  A: 

Probably because your command returns on CanExecute() false, because it has no command target.

Pop Catalin
hello Pop Catalin,I have commandTarget line in XAML file as below...CommandTarget="{Binding ElementName=MyCanvas}">
KJ
As I see you are you creating the target canvas in code, after the button is added to the toolbar? that will cause the binding to fail if it will not find the target element when the binding is instantiated...
Pop Catalin
Set the CommandTarget in XAML to MyGrid (routed commands traverse the logical tree until they find an element that can execute the command).
Pop Catalin
okay, should i need to have all the routedcommand in mygrid or still in canvas control?
KJ
A: 

Have you tried in the CanExecute handler setting the event has handled?

private void AddNewTab_Enabled(object sender, CanExecuteRoutedEventArgs e)    
{        
    e.CanExecute = true;    
    e.Handled = true
}
Steven