C#/WPF:为什么选项卡无法正确聚焦

发布于 2024-09-28 09:18:57 字数 1316 浏览 2 评论 0 原文

我有一个选项卡控件

<TabControl Height="Auto" Grid.Row="1" ItemsSource="{Binding Tabs}" IsSynchronizedWithCurrentItem="True">

,它绑定到 ViewModel 中的 Tabs。我还使用 CollectionViewSource 来聚焦选项卡

protected ObservableCollection<TabViewModel> _tabs;
protected ICollectionView _tabsViewSource;

public ObservableCollection<TabViewModel> Tabs
{
    get { return _tabs; }
}
public void OnTabsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null && e.NewItems.Count > 0)
        foreach (TabViewModel tab in e.NewItems)
        {
            tab.CloseRequested += OnCloseRequested;
            _tabsViewSource.MoveCurrentTo(tab); // focus newly created tab
        }
    if (e.OldItems != null && e.OldItems.Count > 0)
        foreach (TabViewModel tab in e.OldItems)
            tab.CloseRequested -= OnCloseRequested;
}

当我有超过 1 个选项卡时,当我创建新选项卡时,选项卡会正确聚焦

当没有选项卡时,新选项卡似乎无法正确聚焦。请注意选项卡标题

alt text

我该如何解决此问题?或者是什么导致了这种行为?显示文本框(选项卡内容),但标题不像其选择的那样呈现

更新

它适用于新文件/项目...嗯...必须是一些相关代码...我可能会重做该部分.. 。

I have a tab control

<TabControl Height="Auto" Grid.Row="1" ItemsSource="{Binding Tabs}" IsSynchronizedWithCurrentItem="True">

That is bound to Tabs in the ViewModel. I also used CollectionViewSource to focus tabs

protected ObservableCollection<TabViewModel> _tabs;
protected ICollectionView _tabsViewSource;

public ObservableCollection<TabViewModel> Tabs
{
    get { return _tabs; }
}
public void OnTabsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null && e.NewItems.Count > 0)
        foreach (TabViewModel tab in e.NewItems)
        {
            tab.CloseRequested += OnCloseRequested;
            _tabsViewSource.MoveCurrentTo(tab); // focus newly created tab
        }
    if (e.OldItems != null && e.OldItems.Count > 0)
        foreach (TabViewModel tab in e.OldItems)
            tab.CloseRequested -= OnCloseRequested;
}

When I have more that 1 tab, when I create new Tabs, tabs are focused properly

alt text

when there are no tabs, new tabs don't seem to be focused properly. notice the tab header

alt text

how might I fix this? or what is causing this behavior? the text box (tab content) is shown but the header don't render like its selected

UPDATE

It works with a fresh file/project ... hmm ... must be some related code ... I might redo that part ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2024-10-05 09:18:57

除非将 TabControl.ItemsSource 绑定到 ICollectionView,否则 IsSynchronizedWithCurrentItem="True" 没有任何意义。

我无法判断将绑定从 ObservableCollection 更改为 ICollectionView 是否可以解决您的问题,但这就是我设置数据绑定选项卡控件的方式。

另一种方法是公开一个新属性

public TabViewModel CurrentTabViewModel
{
    get
    {
        return _tabs.CurrentItem as TabViewModel:
    }
    set
    {
        _tabs.MoveCurrentTo(value);
    }
}

并将 TabControl 的 SelectedItem 绑定到 CurrentTabViewModel

<TabControl SelectedItem="{Binding Path=CurrentTabViewModel}" ... />

IsSynchronizedWithCurrentItem="True" has no meaning unless you bind your TabControl.ItemsSource to an ICollectionView.

I can't tell if changing your binding from ObservableCollection to ICollectionView will solve your problem, but that is how I have setup my databound tabcontrol.

An alternative could be to expose a new property

public TabViewModel CurrentTabViewModel
{
    get
    {
        return _tabs.CurrentItem as TabViewModel:
    }
    set
    {
        _tabs.MoveCurrentTo(value);
    }
}

and bind TabControl's SelectedItem to CurrentTabViewModel

<TabControl SelectedItem="{Binding Path=CurrentTabViewModel}" ... />
本宫微胖 2024-10-05 09:18:57

如果没有初始化单选项卡集合的代码,它只是猜测。
解决方法是设置 tabView 的 SelectedIndex = 0 ->最初选择第一个选项卡。

<TabControl Height="Auto" 
  Grid.Row="1" 
  ItemsSource="{Binding Tabs}" 
  IsSynchronizedWithCurrentItem="True" 
  SelectedIndex="0">

Without the code, that initializes the single-tab-collection, it's just guessing.
A Workaround for you would be setting SelectedIndex of the tabView = 0 -> first tab is selected initially.

<TabControl Height="Auto" 
  Grid.Row="1" 
  ItemsSource="{Binding Tabs}" 
  IsSynchronizedWithCurrentItem="True" 
  SelectedIndex="0">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文