How to Programmatically Create a ToolBar with Buttons in C#?

vitaminchik 486 Reputation points
2023-04-17T20:33:55.2533333+00:00

Hello. I am creating an application window through C# code. How can I add a ToolBar to a page using C#? Programmatically, I do because I have a table and I add new rows. I know how to do it through xml, but how through C#?

            ToolBar myToolBar = new ToolBar();
            Grid.SetRow(myToolBar,0);
            myToolBar.VerticalAlignment= VerticalAlignment.Top;
            myToolBar.Background= new SolidColorBrush(Color.FromRgb(238, 245, 253));
            myToolBar.Height= 50;
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2023-04-18T02:15:09.5333333+00:00

    Hi,@vitaminchik. Welcome Microsoft Q&A. You could refer to the following code. MainWindow.xaml:

     <StackPanel x:Name="sp">
            <Frame x:Name="myFrame" Width="450" Height="200"/>
        </StackPanel>
    
    

    MainWindow.xaml.cs:

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Page1 page1 = new Page1();
                myFrame.Navigate(page1);
            }
        }
    

    Page1.xaml:

     <Grid Background="AliceBlue" Name="grid">
            
        </Grid>
    

    Page1.xaml.cs:

     public partial class Page1 : Page
        {
            public Page1()
            {
                InitializeComponent();
                grid.Children.Add( CreatetoolBarTray());
            }
            public ToolBarTray CreatetoolBarTray()
            {
                var tbt = new ToolBarTray
                {
                    Width = 450.0,
                    IsLocked = true
                };
                var tb = new ToolBar();
                var b = new Binding
                {
                    Path = new PropertyPath("ActualWidth"),
                    Source = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ToolBarTray), 1),
                };
                tb.SetBinding(WidthProperty, b);
    
                tb.Items.Add(new Button() { Content = "b1"  , Width=50,Background=Brushes.LightGray});
                tb.Items.Add(new Button() { Content = "b2" ,Width = 50, Background = Brushes.LightGray });
    
                tbt.ToolBars.Add(tb);
    
                return tbt;
            }
        }
    
    

    The result: User's image


    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. [5]: https://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.html


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.