简体   繁体   English

更改SelectedIndex时未选择TabControl标头

[英]TabControl header not selected when SelectedIndex changed

First, I found this answer and initially thought it solved my problem: Binding to TabControl SelectedIndex 首先,我找到了这个答案,最初以为它解决了我的问题: 绑定到TabControl SelectedIndex

The screenshots of the tab-header in that post show what I'm seeing with my tabs. 该帖子中的选项卡标题的屏幕快照显示了我在选项卡中看到的内容。

In particular, adding the check if(value != _SelectedTabIndex) before updating the property and raising the PropertyChanged notification seemed to work. 特别是,在更新属性和引发PropertyChanged通知之前添加检查if(value!= _SelectedTabIndex)似乎可行。 By that I mean, it made my first added tab display correctly (with the header also selected). 我的意思是,它使我第一个添加的标签正确显示(还选择了标题)。

However, this only works for the first item added to the TabControl. 但是,这仅适用于添加到TabControl的第一个项目。 When I add a second item, neither the first or second header is selected, however the second item is visible in the body of the tab, meaning that it sort of works. 当我添加第二个项目时,第一个或第二个标题都未被选中,但是第二个项目在选项卡的主体中可见,这意味着它可以正常工作。

Here is my XAML: 这是我的XAML:

<TabControl Grid.Row="1" Grid.Column="2" ItemsSource="{Binding TabViewModels}"
            SelectedIndex="{Binding SelectedTabIndex, Mode=OneWay}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding TabName}" />
            <Setter Property="Content" Value="{Binding}" />
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

Here is my CollectionChanged handler: 这是我的CollectionChanged处理程序:

private void OnTabCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // Set selected index to last index in collection
    SelectedTabIndex = TabViewModels.Count - 1;
}

And here is my property: 这是我的财产:

private int _SelectedTabIndex;
public int SelectedTabIndex
{
    get { return _SelectedTabIndex; }
    set
    {
        if(value != _SelectedTabIndex)
        {
            _SelectedTabIndex = value;
            OnPropertyChanged();
        }
    }
}

Okay, so I found what the problem is, but I don't know the best solution. 好的,所以我找到了问题所在,但我不知道最佳解决方案。 The CollectionChanged event is raised before the TabControl is updated with the new items. 在用新项更新TabControl之前,引发CollectionChanged事件。 This means setting the SelectedTabIndex to the last item is, for a brief moment, out of bounds (until the TabControl's items are updated). 这意味着在短时间内将SelectedTabIndex设置为最后一项是超出范围的(直到TabControl的项被更新)。

The sequence is like so: 1. Add item to collection 2. CollectionChanged event is raised 3. SelectedTabIndex is set to last item (TabControl doesn't have the recently added item yet) 4. TabControl items are updated 顺序如下:1.将项目添加到集合中2.引发CollectionChanged事件3. SelectedTabIndex设置为最后一个项目(TabControl还没有最近添加的项目)4. TabControl项目已更新

I proved this by creating a Task in the CollectionChanged event handler that delays 10ms, then sets the SelectedTabIndex, but this seems sloppy. 我通过在CollectionChanged事件处理程序中创建一个延迟10毫秒的任务,然后设置SelectedTabIndex来证明这一点,但这似乎草率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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