以编程方式将按钮添加到wpf中的工具栏

本文关键字:wpf 工具栏 添加 编程 方式 按钮 | 更新日期: 2024-10-19 12:52:42

我正试图在浏览器中添加书签功能,但我不知道如何将创建的按钮添加到工具栏中。

我的工具栏在XAML中定义:

<ToolBar x:Name="Bookbar" HorizontalAlignment="Left" VerticalAlignment="Top" Width="799" Height="36">
        </ToolBar>

以及我尝试将创建的按钮添加到工具栏的c#代码:

private void Bookmark_Click(object sender, RoutedEventArgs e) //add bookmark
    {
        if (Urlbox.Text.Contains("http://") == false)
        {
            bookmarks = Microsoft.VisualBasic.Interaction.InputBox("Please insert your new bookmark", "New Bookmark", "http://" + Urlbox.Text);
        }
        else
        {
            bookmarks = Microsoft.VisualBasic.Interaction.InputBox("Please insert your new bookmark", "New Bookmark", Urlbox.Text);
        }

        Button book1 = new Button();
        book1.Content = "Test";
        book1.Click += Button_Click;
        Bookbar.Children.Add(book1);
    }

我收到

Error   1   'System.Windows.Controls.ToolBar' does not contain a definition for 'Children' and no extension method 'Children' accepting a first argument of type 'System.Windows.Controls.ToolBar' could be found (are you missing a using directive or an assembly reference?)

在"Bookbar.Children.Add(book1);"一行

我做错了什么?

以编程方式将按钮添加到wpf中的工具栏

它不是BookBar.Children.Add(),它应该是BookBar.Items.Add