我想到了。我从 DocumentPaneTabPanel 更改了 ArrangeOverride(Size finalSize) 方法中的代码,以防止将当前选项卡重新定位到第一个位置。
它现在隐藏当前不适合面板的选项卡左侧的选项卡 - 首先隐藏最左侧的选项卡。当前选项卡右侧的选项卡也被隐藏 - 这里最右边的选项卡首先隐藏。如果存在溢出,则当前选项卡始终是面板上的最右侧。目前这有点脏,但我认为以更好的方式实现这一点应该不会那么难。
这是代码:
protected override Size ArrangeOverride(Size finalSize)
{
double offset = 0.0;
var children = Children.Cast<UIElement>().Where(ch => ch.Visibility != System.Windows.Visibility.Collapsed).ToList();
if (children.Count > 0)
{
//find currently selected tab
int i = 0;
for (i = 0; i < children.Count(); i++)
{
TabItem doc = (TabItem)children[i];
var layoutContent = doc.Content as LayoutContent;
if (layoutContent.IsSelected)
break;
}
//calculate how many tabs left from the currently selected would fit in the panel
int cur_ind = i;
TabItem current_item = (TabItem)children[cur_ind];
List<TabItem> t_to_display = new List<TabItem>();
while (cur_ind >= 0 && offset + current_item.DesiredSize.Width <= finalSize.Width)
{
current_item = (TabItem)children[cur_ind];
current_item.Visibility = System.Windows.Visibility.Visible;
offset += current_item.DesiredSize.Width + current_item.Margin.Left + current_item.Margin.Right;
t_to_display.Add(current_item);
--cur_ind;
}
//arrange the fitting tabs on the left
double cur_offset = offset;
foreach (TabItem t in t_to_display)
{
cur_offset -= t.DesiredSize.Width + t.Margin.Left + t.Margin.Right;
t.Arrange(new Rect(cur_offset, 0.0, t.DesiredSize.Width, finalSize.Height));
}
//arrange the tabs on the right
cur_ind = i + 1;
while (cur_ind < children.Count && offset + current_item.DesiredSize.Width <= finalSize.Width)
{
current_item = (TabItem)children[cur_ind];
current_item.Visibility = System.Windows.Visibility.Visible;
current_item.Arrange(new Rect(offset, 0.0, current_item.DesiredSize.Width, finalSize.Height));
offset += current_item.DesiredSize.Width + current_item.Margin.Left + current_item.Margin.Right;
cur_ind++;
}
while(cur_ind < children.Count)
{
current_item = (TabItem)children[cur_ind];
current_item.Visibility = System.Windows.Visibility.Hidden;
cur_ind++;
}
}
return finalSize;
}