知道为什么我的usercontrol没有以tabcontrol为中心吗?

时间:2012-04-06 01:22:12

标签: c# wpf

我有点困惑,为什么这不起作用,因为我让它在原型中工作,我认为唯一的重大区别是我使用自定义TabItem和UserControl而不是默认的。我正在尝试获取看似在选项卡窗口中居的usercontrol,但它似乎是左对齐。

您可以将此方法用于您要使用的用户控件,并将其格式化并将其粘贴到tabcontrol中。在我之前做过的测试解决方案中,设置scroll的水平和垂直对齐以拉伸固定它,但在这种情况下它不起作用。是否有某些其他设置或某些东西会覆盖这个?

public void CreateNewTab(UserControlGeneric new_user_control, string tab_header)
    {
        //TabItem tab = new TabItem();
        TabItemIndexed tab = new TabItemIndexed();
        //The scrollviewer is created/setup to make sure the usercontrol gets scroll bars if the window if ever made smaller than the usercontrol
        ScrollViewer scroll = new ScrollViewer();

        //How you programatically set a scrollviewer's height and width to be "Auto"
        scroll.Height = Double.NaN;
        scroll.Width = Double.NaN;

        scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
        scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        scroll.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        scroll.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

        scroll.Content = new_user_control;
        tab.Content = scroll;
        tab.Header = tab_header;

        //If there aren't any tabs, then hide the "No Workspaces Open" notice (Since we're adding a tab)
        if (!tabControl_main.HasItems) label_no_workspaces_open.Visibility = System.Windows.Visibility.Hidden;

        tabControl_main.Items.Add(tab);
        tabControl_main.SelectedItem = tab;

    }

0 个答案:

没有答案