ToolBarTray and ToolBar Example


WPF provides a ToolBarTray into which you may place re-sizable Toolbars. These Toolbars may themselves contain buttons. When you re-size the Toolbar too small to display the buttons, then the buttons will disappear but will be available via a little dropdown arrow.

See Button Example for more options with regards to creating the buttons. For instance, you could create a toolbar button with a vector graphic or image inside it. It doesn't have to be just text.

//1. Create Toolbar
ToolBar tbar1 = new ToolBar();
tbar1.Initialize(500D, 50D, ContainerType.HorizontalPanel);
tbar1.Background = Brushes.Orange;

//2. Create simple buttons with onmouseover colors
Button tbtn1 = new Button();
tbtn1.Initialize(130D, 40D, ContainerType.ContentControlPanel);
tbtn1.ApplyButtonStrategy("White", "Gray", "Khaki", "Black", "Orange", "Gold", new Thickness(0), System.Windows.VerticalAlignment.Center, System.Windows.HorizontalAlignment.Center);
Button tbtn2 = new Button();
tbtn2.Initialize(130D, 40D, ContainerType.ContentControlPanel, "White", "Gray", "Khaki", "Black", "Orange", "Gold", new Thickness(0), System.Windows.VerticalAlignment.Center, System.Windows.HorizontalAlignment.Center);

//3. Add Settings to Toolbar for the Buttons
           .Set<Button, ToolBar>(0, 0, "Width", 130D)
           .Set<Button, ToolBar>(0, 0, "Height", 40D)
           .Set<Button, ToolBar>(0, 0, "Content", "Get values from Grid")
           .Set<Button, ToolBar>(0, 1, "Width", 130D)
           .Set<Button, ToolBar>(0, 1, "Height", 40D)
           .Set<Button, ToolBar>(0, 1, "Content", "Add new row to Grid")
           .Set<Button, ToolBar>(0, 2, "Width", 130D)
           .Set<Button, ToolBar>(0, 2, "Height", 40D)
           .Set<Button, ToolBar>(0, 2, "Content", "Delete selected row")

//4. Add Buttons (via a Composite) to Toolbar and Subscribe to Button Clicks
           .AddExisting<Button, ToolBar>(0, 0, tbtn1)
           .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 0, "PreviewMouseDown", ((o, ev) =>{
                       //code for button tbtn1 click . . .
            .AddExisting<Button, ToolBar>(0, 1, tbtn2)
            .SubscribeEventOnChild<Button, ToolBar, MouseButtonEventArgs>(0, 1, "PreviewMouseDown", ((o, ev) =>
                        //code for button tbtn2 click . . .
             .EndComposite<ToolBar, ToolBarArgs>(null);

//5. Create ToolBarTray and Add Toolbar to Tray
ToolBarTray tTray = new ToolBarTray();
tTray.Background = Brushes.Gray;
tTray.Initialize(790D, 100D, ContainerType.HorizontalPanel, Orientation.Horizontal, false);
     .AddExisting<ToolBar, ToolBarTray>(0, 0, tbar1)
     .EndComposite<ToolBarTray, ToolBarTrayArgs>(new ToolBarTrayArgs(0, 1));

The ToolbarTrayArgs allow you to set a band number (0, 1, 2 ... etc.) which is the order in which the Toolbars are stacked from top (0) to next (1) to next (2) . . . etc. to bottom.

The ToolbarTrayArgs also allows you to set an index number within a band itself. This is the order the Toolbars will appear in within a band, if you add multiple Toolbars within a single band. This order starts from left (0) to right (1) to right (2) . . . etc.

Last edited May 25, 2014 at 9:11 PM by stagathome0069, version 8


No comments yet.