从另一个用户控件添加TabItem

时间:2012-06-07 21:33:02

标签: c# wpf class

我陷入了一个非常基本的问题。

shell窗口上有两个用户控件。第一个基本上有一个色带控制。第二个控件基本上有一个tab控件。

如何在功能区上单击按钮时添加新的标签项?包括Shell在内的所有视图都共享相同的命名空间。

Ribbon.xaml

<UserControl x:Name="RibbonControl" ... >

WorkSpace.xaml

<UserControl x:Name="WorkSpaceControl" ... >

Shell.xaml

<RibbonWindow  x:Name="ShellWindow" ... >
    ...
    <Views:RibbonRegion x:Name="RibbonControl" />
    <Views:WorkspaceRegion  x:Name="WorkSpaceControl" />
    ...
</RibbonWindow>

Ribbon.xaml.cs

namespace Application.Views
{
  public partial class RibbonControl: UserControl
   {

    private void Button_Click(object sender, RoutedEventArgs e)
    {

        //This throws the error that name is not available in current context.
        WorkSpaceControl.AddDataTab("Hede");   


        //This doesn't throw error. But I think because the tab control
        //has already initialized, it doesn't add the tab neither.
        TabRegion su = new TabRegion();
        su.AddDataTab("Hede");

    }
  }
}

1 个答案:

答案 0 :(得分:0)

睡了个好觉之后,答案是:

Shell MainFrm = (Shell)App.Current.MainWindow;
MainFrm.WorkSpaceControl.AddDataTab("Something");