<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