选择了WPF选项卡控件项,但ui不会更改

时间:2014-08-08 17:56:10

标签: c# wpf xaml tabs

我有这个自定义标签控件,除了一件事,它到目前为止工作得很好。该选项卡控件允许用户将选项卡取消停靠为窗口并重新停靠选项卡。该选项卡可以解锁,但是当用户重新停靠时,它会添加该项目,但不会显示它已被选中。

以下是重新对接的代码......

//find window
        DependencyObject window = sender as UIElement;
        while (!(window is Window) && window != null)
        {
            window = VisualTreeHelper.GetParent(window);
        }
        Window currentWindow = window as Window;

        //redock window
        if (CurrentTabControl != null)
        {
            TabItem tab = new TabItem();

            //add header text
            tab.Header = currentWindow.Title;

            //add frame to tab
            tab.Content = currentWindow.Content;

            //add tab to tab control
            CurrentTabControl.Items.Add(tab);

            //select tab
            CurrentTabControl.SelectedItem = tab;
        }

        //close window
        currentWindow.Close();

现在,对于标签控件,此项目实际上已停靠。它调用选项卡控件上的选择更改事件。但是由于某种原因,ui并没有改变。仍然以这种方式选择另一个项目。我不知道我是否可以对此进行更新或者更新,但它很奇怪。

谢谢!

///// EDIT ///////

因此,第一段代码是从我制作的自定义窗口上的按钮调用的。我不知道这是否有任何关系。我还有一个方法,可以将页面添加到选项卡控件,并且工作正常。这是......

public Tuple<TabItem, Page> AddNewPage(Type pageType, string header)
    {
        //create new tab
        TabItem tab = new TabItem();

        //add header text
        tab.Header = header;

        //create new frame
        Frame frame = new Frame();

        //create new Page
        Page page = (Page)Activator.CreateInstance(pageType);

        //navigate to page
        frame.Navigate(page);

        //add frame to tab
        tab.Content = frame;

        //add tab to tab control
        CurrentTabControl.Items.Add(tab);

        //select tab
        CurrentTabControl.SelectedItem = tab;

        return new Tuple<TabItem, Page>(tab, page);
    }  

///// SOLUTION ////// 显然我需要在窗口关闭后选择该选项卡。所以我只是重新组织了一下代码,它的确有效!

//find window
        DependencyObject window = sender as UIElement;
        while (!(window is Window) && window != null)
        {
            window = VisualTreeHelper.GetParent(window);
        }
        Window currentWindow = window as Window;

        TabItem tab = new TabItem();

        //add header text
        tab.Header = currentWindow.Title;

        //add frame to tab
        tab.Content = currentWindow.Content;

        //close window
        currentWindow.Close();

        //redock window
        if (CurrentTabControl != null)
        {
            //add tab to tab control
            CurrentTabControl.Items.Add(tab);

            //select tab
            CurrentTabControl.SelectedItem = tab;
        }

0 个答案:

没有答案