且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF SelectedIndex 设置 TabControl 的问题

更新时间:2023-12-06 17:14:52

这是因为您正在 SelcetedIndexChanged 事件中更改 selectedIndex,该事件将以同步方式调用自身.而是尝试以这样的异步方式将它放在 UI 调度程序上 -

This is because you are changing the selectedIndex within the SelcetedIndexChanged event which will call itself in sycnhronous manner. Instead try to put it on UI dispatcher in an aysnchronous manner like this -

private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   Debug.WriteLine("Selected Index: " + tab.SelectedIndex);

   if (tab.SelectedIndex == 1)
   {
      Application.Current.Dispatcher.BeginInvoke
          ((Action)delegate { tab.SelectedIndex = 0; }, DispatcherPriority.Render, null);
   }
}

它会给你想要的输出.