我有一个使用Avalon Dock 2.0作为停靠管理器的WPF应用程序。我正面临着Avalon Dock正在执行的新打开标签的标准定位问题。
只要所有标签都适合标签栏,标签栏最右侧的位置就会附加一个新标签。只要新选项卡不适合栏,就会在最左侧的位置添加新选项卡,使前一个最右边的选项卡消失。
我知道这是Visual Studio标准行为,但在我的应用程序中,顺序有意义。这意味着应始终在最左侧或最右侧位置添加新选项卡。该开关对用户来说非常混乱。
有没有办法让Avalon Dock总是在最左边或最右边的位置添加一个新标签?
答案 0 :(得分:4)
我明白了。我从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;
}