简体   繁体   中英

WPF User Control with Tab Control

I'm attempting to create a user control that houses a tab control.

My question is, how do I expose the tab control through the user control so we can add tabs?

The entire control will house three areas: command buttons at the top, the tab control, and a styled textblock that displays messages.

The command buttons will be configurable as dependencyproperties, so we may choose to show the apply button or not when we use the usercontrol. I'm strictly a designer and not a developer, so I'm trying to get my feet wet in building this control, but I'm in a little over my head.

I have no problem whipping up the XAML for what I'm trying to accomplish, just having a hard time making it reusable.


More details on what I have tried so far (haven't tried anything in the answers just yet).

I attempted to add a dependencyproperty that would expose the collection for the tabcontrol itemssource:

public IEnumerable<object> TabSource
    {
        get { return (IEnumerable<object>)GetValue(TabSourceProperty); }
        set { base.SetValue(FunctionPanel.TabSourceProperty, value); }
    }

    public static DependencyProperty TabSourceProperty = DependencyProperty.Register(
         "TabSource",
         typeof(IEnumerable<object>),
         typeof(FunctionPanel));

And then bind to it in the user control XAML:

<TabControl Grid.Row="1" ItemsSource="{Binding TabSource}" />

Finally, I would like to use it in the window XAML like so:

<local:FunctionPanel>
  <local:FunctionPanel.TabSource>
    <TabItem Header="Test" />
  </local:FunctionPanel.TabSource>
</local:FunctionPanel>

But this returns TabSource is unrecognizable or unaccessible. I will attempt the solutions provided below.

My question is, how do I expose the tab control through the user control so we can add tabs?

One straight forward option would be adding a public method to the user control:

public void AddTab(string header)
{
    this.tabControl.Items.Add(header);
}

another option would be to expose the Items property on the user control:

public ItemCollection Items
{
    get { return this.tabControl.Items; }
}

I provide you with a sample to open a Page inside a Tab Control , hope it helps :

public void OpenTabForm(Page oPage)
        {
            try
            {
                Frame oFrame = new Frame();
                oFrame.Content = oPage;

                TabItem myItem = new TabItem();

                myItem.Header = oPage.Name; //give the header text
                myItem.Content = oFrame;

                tbtabMain.Items.Add(myItem);

                tbtabMain.SelectedItem = myItem;
            }
            catch (Exception ex)
            {
                //handle error
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM