如何以编程方式在 WPF TabControl 中选择 TabItem

发布于 2024-12-12 13:46:36 字数 235 浏览 0 评论 0 原文

我想知道如何在 WPF TabControl 中选择特定的 TabItem。

我尝试了以下这些但没有任何效果!

MyTabControl.SelectedIndex = x

MyTabControl.SelectedItem = MyTabItem

MyTabControl.SelectedValue = MyTabItem

MyTabItem.IsSelected = True

I would like to know how to select a specific TabItem in a WPF TabControl.

I tried these bellow but nothing work!

MyTabControl.SelectedIndex = x

MyTabControl.SelectedItem = MyTabItem

MyTabControl.SelectedValue = MyTabItem

MyTabItem.IsSelected = True

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

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

发布评论

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

评论(9

野侃 2024-12-19 13:46:37

正如 @Chris 所说,前三件事中的任何一个都应该有效,而正如 @Phyxx 所说,它并不总是真正有效。问题在于属性更改顺序的一些微妙之处。要解决这个问题,您需要让 WPF 在自己的时间调用您的选项卡选择代码:

Dispatcher.BeginInvoke((Action)(() => MyTabControl.SelectedIndex = x));

这正是 Phyxx 计时器的作用,但方式稍微不那么极端。

As @Chris says, any of the first three things should work and as @Phyxx says, it doesn't always really work. The problem is some subtle thing about the order of property changes. To work around it you need to let the WPF invoke your tab-selection code in its own time:

Dispatcher.BeginInvoke((Action)(() => MyTabControl.SelectedIndex = x));

This does just what Phyxx' timer does, but in a slightly less extreme way.

小情绪 2024-12-19 13:46:37

除了第三个之外,您的所有示例都是正确的并且可以工作。问题一定出在另一个位置。也许您在设置后重置了该项目,或者您的代码从未被调用?

有效

MyTabControl.SelectedIndex = x   
MyTabControl.SelectedItem = MyTabItem    
MyTabItem.IsSelected = True 

无效

MyTabControl.SelectedValue = MyTabItem 

All your examples except the third one are correct and will work. The problem must be at another location. Maybe you reset the item after setting or your code never is called?

Valid

MyTabControl.SelectedIndex = x   
MyTabControl.SelectedItem = MyTabItem    
MyTabItem.IsSelected = True 

Invalid

MyTabControl.SelectedValue = MyTabItem 
绮烟 2024-12-19 13:46:37

循环遍历 TabItems 并选择要选择的选项卡,

tabItem.IsSelected = true

如果由于绑定更改而有任何其他地方,您将看到问题。否则,上面的代码应该可以工作。

Loop through the TabItems and for the tab to be selected, set

tabItem.IsSelected = true

If there are any other place due to binding changing you will see problem. Otherwise, the above code should work.

单调的奢华 2024-12-19 13:46:37

上面没有提到的一件事:

像这样的东西不起作用的主要原因是选项卡项没有设置“名称”属性。要以编程方式导航到的选项卡控件的每个选项卡项都必须设置其名称属性,以便上述任何代码都能工作。

<tabItem Name="tab1"></tabItem>

One thing which hasn't been mentioned above:

The main reason something like this won't work is that the tab items do not have the "Name" property set. Each tab item of the tab control which you want to navigate to programmatically must have its name property set for any of the above code to work.

<tabItem Name="tab1"></tabItem>
穿越时光隧道 2024-12-19 13:46:37

我已经实现了一个基于 MVVM 绑定的小型解决方案,用于实用地选择选项卡面板。

  1. 在视图模型中定义属性 - 选定的 int 类型

  2. 在视图中绑定属性

    <前><代码>

  3. private int _selected;

    public int 已选择
    {
        获取{返回_selected; }
        放
        {
            _选定=值;
            OnPropertyChanged("选定");
        }
    }
    
  4. 将值设置为Select属性,只需绑定即可激活选项卡面板。

    如果您想从父选项卡面板内的选项卡面板进行导航,此解决方案将简单有效,您所需要做的就是访问控件的数据上下文并进行设置

    // 设置指向选项卡控制器索引的视图模型的属性值。
    ((CandidateViewModel)((System.Windows.FrameworkElement)candidateTab.Content).DataContext).Selected = CandidateLogTabIndex;
    

I have implemented a small MVVM bindings based solution for selecting tab panels pragmatically.

  1. define a property in your view model - Selected int type

  2. bind the property in your view

    <TabControl
        x:Name="TabsCandidate" 
        VerticalAlignment="Stretch" 
        TabStripPlacement="Top"
        SelectedIndex="{Binding Selected}"
    
  3. private int _selected;

    public int Selected
    {
        get { return _selected; }
        set
        {
            _selected = value;
            OnPropertyChanged("Selected");
        }
    }
    
  4. Set the value to Select property, simply the binding will activate the tab panel.

    if you want to navigate from tab panel inside parent tab panels, this solution will simply works, All you need to do is, access the data context of your control and set it

    // set the property value of the view model which points the index of the tab controller.
    ((CandidateViewModel)((System.Windows.FrameworkElement)candidateTab.Content).DataContext).Selected = CandidateLogTabIndex;
    
梦在夏天 2024-12-19 13:46:37

尝试在 UI 的 DataContextChangedLoaded 的事件处理程序中设置 MyTabControl.SelectedIndex = x 。希望这会起作用。

Try to set the MyTabControl.SelectedIndex = x in the event handler of DataContextChanged or Loaded of your UI. Hope this will work.

感性 2024-12-19 13:46:37

我尝试了所有应该有效的方法,但就像你一样,实际上没有任何改变所选的选项卡。最后,我通过将选项卡选择代码放入 DispatcherTimer 刻度中使其工作。

       DispatcherTimer switchTabTimer = new DispatcherTimer();
       switchTabTimer.Interval = new TimeSpan(0);
       switchTabTimer.Tick += (object timerSender, EventArgs timerE) =>
       {
           myTabControl.SelectedIndex = 0;
           switchTabTimer.Stop();
       };
       switchTabTimer.Start(); 

I tried all the methods that should have worked, but like you nothing actually changed the selected tab. In the end I got it to work by putting the tab selection code in a DispatcherTimer tick.

       DispatcherTimer switchTabTimer = new DispatcherTimer();
       switchTabTimer.Interval = new TimeSpan(0);
       switchTabTimer.Tick += (object timerSender, EventArgs timerE) =>
       {
           myTabControl.SelectedIndex = 0;
           switchTabTimer.Stop();
       };
       switchTabTimer.Start(); 
彼岸花ソ最美的依靠 2024-12-19 13:46:37

如果您不知道选项卡的索引(提示其不是TabIndex),请使用:

    private async Task ChangeTabTo(TabItem wantedTab) {
        int index = 0;
        for (var i = 0; i < TabControl.Items.Count; i++) {
            var tab = TabControl.Items[i];
            var t = tab as TabItem;
            if (t == null) continue;
            if (t == wantedTab) {
                index = i;
                break;
            }
        }

        await Dispatcher.BeginInvoke((Action)(() => TabControl.SelectedIndex = index));
    }

或者如果您不想保留引用,则将其修改为按名称搜索到选项卡

if you don't know the index of the tab (hint its not TabIndex) use:

    private async Task ChangeTabTo(TabItem wantedTab) {
        int index = 0;
        for (var i = 0; i < TabControl.Items.Count; i++) {
            var tab = TabControl.Items[i];
            var t = tab as TabItem;
            if (t == null) continue;
            if (t == wantedTab) {
                index = i;
                break;
            }
        }

        await Dispatcher.BeginInvoke((Action)(() => TabControl.SelectedIndex = index));
    }

or modify it to search by name if you don't want to keep a reference to the tab

泪痕残 2024-12-19 13:46:37

我在这个话题上投入了我的 2 美分,因为它可能会帮助别人。我正在使用带有 Prims 框架的 WPF。

我无法通过绑定到 SelectedItemSelectedIndex 来选择选项卡 - 它不起作用。我也无法从 TabControl.ItemTemplateTabControl.ContentTemplate 中设置 TabItem.Name 值。

相反,我实现了基于事件的解决方案:

  1. 为我的 TabControl 添加名称值。
  2. 创建一个事件 - 在 Prism 中,这意味着定义一个派生自 PubSubEvent 的类(T 是参数类型 - 在我的例子中,它是绑定到 TabItem> 的 ViewModel 对象。
  3. 发布该事件每当我想要选择一个选项卡时,
  4. 订阅 View.cs 类中的事件并使用 FindName 以编程方式设置 TabControl.SelectedItem

I'm throwing my 2 cents on the topic, since it might help someone out. I'm using WPF with Prims framework.

I was unable to select a tab by binding to SelectedItem or SelectedIndex - it didn't work. I was also unable to set TabItem.Name value from within TabControl.ItemTemplate or TabControl.ContentTemplate.

Instead I implemented event-based solution:

  1. Add Name value for my TabControl.
  2. Create an event - in Prism that means define a class that derives from PubSubEvent<T> (T is the type of parameter - in my case that was the ViewModel object bound to the TabItem>.
  3. Publish that event whenever I want to a tab to be selected.
  4. Subscribe to the event within my View.cs class and set the TabControl.SelectedItem programmatically using FindName.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文