窗口关闭后重置WPF TabControl SelectedItem

时间:2010-09-05 13:17:13

标签: .net wpf

我对WPF-TabControl中的所选项目有疑问,可以使用以下简化代码进行复制:

如果通过第二个窗口中按钮的Click事件创建并选择了新的TabItem,则会创建,添加和选择该选项卡。当第二个窗口关闭时,选项卡控件的选定项目将重置为最后选择的项目。问题发生在我的MVVM-app中,它独立于Items-collection。我可以使用ItemsSource或Items-Collection,它总是发生。

有人知道这里发生了什么或有一个很好的解决方法吗?

TabControl tabControl = new TabControl() ;
tabControl.Items.Add(new TabItem { Header="InitialTabItem"});
((TabItem)tabControl.Items[0]).Focus();
Window mainWindow = new Window() { Content=tabControl,Title="TabControl-Window"};
mainWindow.Show();            
Button addButton = new Button() { Content="AddTabItem"};
addButton.Click += (o, e) => {
        TabItem newTabItem=new TabItem(){Header=DateTime.Now.ToLongTimeString()};
        tabControl.Items.Add(newTabItem);                
        tabControl.SelectedItem = newTabItem;                
};
Window directorWindow = new Window() { Owner = mainWindow ,Content=addButton,Height=80,Width=200,Title="DirectorWindow"};
directorWindow.Show();

更新

当我遇到更大的WPF问题时,似乎一直都是focus-management的问题。如果我更改创建代码如下,它可以工作:

TabItem newTabItem=new TabItem(){Header=DateTime.Now.ToLongTimeString()};
tabControl.Items.Add(newTabItem);                    
Dispatcher.BeginInvoke(new Action(delegate{
    newTabItem.Focus();
    tabControl.SelectedItem = newTabItem;
}), System.Windows.Threading.DispatcherPriority.Input, null);

然而,对我来说,它看起来并不是很自信。一些想法?

2 个答案:

答案 0 :(得分:1)

是的,TabControl有时表现得很奇怪。在我们的项目中,我们必须创建一个子类并覆盖一些方法来解决其中的另一个错误。

在您的情况下,如果您在关注TabControl之前关注TabItem本身,那么一切似乎都有效:

        var tabControl = new TabControl();
        var tabItem = new TabItem { Header = "InitialTabItem" };
        tabControl.Items.Add(tabItem);
        tabControl.Focus();
        tabItem.Focus();
        Window mainWindow = new Window() { Content = tabControl, Title = "TabControl-Window" };
        mainWindow.Show();
        Button addButton = new Button() { Content = "AddTabItem" };
        addButton.Click += (o, args) =>
        {
            TabItem newTabItem = new TabItem() { Header = DateTime.Now.ToLongTimeString() };
            tabControl.Items.Add(newTabItem);
            tabControl.SelectedItem = newTabItem;
        };
        Window directorWindow = new Window() { Owner = mainWindow, Content = addButton, Height = 80, Width = 200, Title = "DirectorWindow" };
        directorWindow.Show();

更新1.查看评论 - 原始代码会导致不良副作用。

        var tabControl = new TabControl();
        var tabItem = new TabItem { Header = "InitialTabItem" };
        tabControl.Items.Add(tabItem);
        Window mainWindow = new Window() { Content = tabControl, Title = "TabControl-Window" };
        mainWindow.Show();
        tabControl.Focus();
        tabItem.Focus();
        Button addButton = new Button() { Content = "AddTabItem" };
        addButton.Click += (o, args) =>
        {
            TabItem newTabItem = new TabItem() { Header = DateTime.Now.ToLongTimeString() };
            tabControl.Items.Add(newTabItem);
            tabControl.SelectedItem = newTabItem;
        };
        Window directorWindow = new Window() { Owner = mainWindow, Content = addButton, Height = 80, Width = 200, Title = "DirectorWindow" };
        directorWindow.Show();

答案 1 :(得分:1)

请参阅Yacoders解决方案,解决了演示代码的问题。然而,这个解决方案引领我的真实项目中的焦点问题。

一种似乎对我有用的方式是我在更新中显示的方式。如果有人遇到同样的问题,请尝试以下代码。到目前为止,我还没有看到任何副作用。

TabControl tabControl = new TabControl() ;  
tabControl.Items.Add(new TabItem { Header="InitialTabItem"});  
((TabItem)tabControl.Items[0]).Focus();  
Window mainWindow = new Window() { Content=tabControl,Title="TabControl-Window"};  
mainWindow.Show();              
Button addButton = new Button() { Content="AddTabItem"};  
addButton.Click += (o, e) => {  
    TabItem newTabItem=new TabItem(){Header=DateTime.Now.ToLongTimeString()};   
    tabControl.Items.Add(newTabItem);                       
    Dispatcher.BeginInvoke(new Action(delegate{   
       newTabItem.Focus();   
       tabControl.SelectedItem = newTabItem;   
    }), System.Windows.Threading.DispatcherPriority.Input, null); 
};  
Window directorWindow = new Window() { Owner = mainWindow ,Content=addButton,Height=80,Width=200,Title="DirectorWindow"};  
directorWindow.Show();