开发者

Tab control like chrome browser in Silverlight Application

开发者 https://www.devze.com 2023-03-12 02:17 出处:网络
I have seen this one before. We want a dedicated tab item with a \'+\' symbol on it. Like chrome browserin Silver light Application.

I have seen this one before. We want a dedicated tab item with a '+' symbol on it. Like chrome browser in Silver light Application.

There is always a tab item appears at the end half visible and once yo开发者_开发知识库u click on it, it turns into a complete tab item.


That was a little though to answer :P

Here it goes:

Create a class called LinqToVisualTree. You can find it at the end of this post, along with an explanation of what it does. Basically, it lets you query your Visual Tree through LINQ.

To add anything to the tabs row in a TabControl, you need to manipulate the TabPanel, which holds the "buttons" of tabs. TabPanel is in the System.Windows.Controls.Primitives namespace, so refer to it.

The easiest way to get the TabPanel I've found is to name at least one of your TabItems and do this:

using System.Windows.Controls.Primitives;       // Contains TabPanel
using LinqToVisualTree;

void AddPlusButton() {
    // Creates a button beside the tabs
    var button = new Button()
    {
        Content = "+",
        IsTabStop = false      // To prevent keyboard press
    };

    // Links the Click with the "new tab" function
    button.Click += new RoutedEventHandler(btPlus_Click);

    // *** HERE IS THE TRICK ***
    // Gets the parent TabPanel in the Visual Tree and cast it
    var tabpn = tabItem1.Ancestors<TabPanel>().FirstOrDefault() as TabPanel;

    // Links the button created
    tabpn.Children.Add(button);
}

Here's the method for the plus button:

void btPlus_Click(object sender, RoutedEventArgs e)
{
    // Creates a new TabItem
    var ti = new TabItem();
    ti.Header = "TabAdded";
    ti.Content = new TextBlock() { Text = "Tab content!" };

    // Links it
    tabControl.Items.Add(ti);
}

That's it! Tip: I've just find out about the TabPanel class using Silverlight Spy. Searching on Google, I could just find methods of doing this by changing the Template Style from the TabControl.

Best regards!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号