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

时间:2013-12-16 09:28:44

标签: c# wpf button browser

我正在尝试向我的浏览器添加书签功能,但我无法弄清楚如何将创建的按钮添加到工具栏中。

我在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);'

这一行

我做错了什么?

1 个答案:

答案 0 :(得分:5)

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

相关问题