且构网

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

tabControl/tabitem 刷新困难

更新时间:2022-01-21 07:16:35

好吧,最后它有一个相当奇怪的行为.如果我这样做

Ok so in the end it has a quite a weird behavious. If I do

for (int i = 0; i < tbcMain.Items.Count; i++)
  {
    tbcMain.SelectedIndex = i;
    tbcMain.UpdateLayout();
  }

它有效.但是我必须设置第一个 tabitem 所以如果我添加

it works. But I have to set the 1st tabitem so if I add

 tbcMain.SelectedIndex = 0;

它没有.所以解决方案是休眠,它再次起作用.

it doesn't. So the solution was put a sleep and it works again.

for (int i = 0; i < tbcMain.Items.Count; i++)
  {
    tbcMain.SelectedIndex = i;
    tbcMain.UpdateLayout();
  }

  System.Threading.Thread.Sleep(250);
  tbcMain.SelectedIndex = 0;

但这一点都不优雅.如果有人有更好的解决方案,请告诉我.顺便说一句,添加 tbcMain.SelectedIndex = 0;在 mainWindow 的加载事件上是没有用的.

But that is not elegant at all. If anyone has a better solution pls let me know it. Btw adding the tbcMain.SelectedIndex = 0; on the loaded event of the mainWindow is of no use.