我想通了。我從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;
}
請問有沒有人有類似的問題? :( –