让WPF Tabcontrol高度假定最大项目的高度? [英] Let WPF Tabcontrol height assume height of largest item?
问题描述
是否可以通过tabcontrol来控制最大选项卡项的大小(实际上是tabitem的内容)?
Is there any way to have to tabcontrol take the size of the largest tab item (well, actually, the tabitem's content)?
由于没有为Tab控件分配特定的大小,因此应该自动调整大小:它可以正确执行操作,但是当您切换标签时,它会自动将其大小调整为当前所选标签内容的高度(和宽度).
Since the tabcontrol has no specific size assigned it should autosize: it does that correctly, but when you switch tabs it automatically resizes itself to the height (and width) of the contents of the currently selected tab.
我不希望调整大小,并让tabcontrol假定最大项目的高度,但仍然可以自动调整大小.
I don't want the resizing to happen, and let the tabcontrol assume the height of the largest item, but still have it autosize itself.
有任何线索吗?我尝试将数据绑定到每个元素的Height
属性,这些元素用作多绑定的内容,同时在Tabcontrol的ActualHeight
和Items
属性上都进行了绑定.但是,可惜,内容元素的ActualHeight
始终为0.
Any clues? I tried databinding to the Height
property of each element used as content to the using a multibinding, with bindings on both the ActualHeight
and the Items
properties of the Tabcontrol. But alas, the ActualHeight
of the content elements is always 0.
<TabItem Header="Core" >
<Grid Margin="5">
<Grid.Height>
<MultiBinding Converter="{Converters1:AllTabContentEqualHeightConverter}">
<Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
<Binding Path="Items" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TabControl}}"/>
</MultiBinding>
</Grid.Height>
...
可以做到吗?
推荐答案
实际上,我认为更容易解决.
由于无论如何我都有一个TabControl
的控件模板,因此我设置了ContentPresenter
的高度,以显示选定的选项卡内容.我使用绑定到TabControl
项的转换器进行此操作,如有必要(使用Measure
)对其进行测量,并检查DesiredSize
我需要的大小.
Actually, it was easier to solve that I thought.
Since I had a controltemplate for the TabControl
anyway, I set the height of the ContentPresenter
presenting the selected tab content. I do this using a converter that binds to the items of the TabControl
, measures them if necessary (using Measure
) and checks DesiredSize
for the size I need.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var items = value as ItemCollection;
if (items == null)
return null;
double max = 0;
foreach (TabItem item in items)
{
var content = item.Content as FrameworkElement;
if (content == null) continue;
if (!content.IsMeasureValid)
content.Measure(new Size(int.MaxValue, int.MaxValue));
var height = content.DesiredSize.Height;
if (max < height)
max = height;
}
return max;
}
这很好用,但有一些警告:
That works just fine, with some caveats:
- 每个标签页内容均应为
FrameworkElement
- 内容在加载后不会更改大小(因为仅在Items属性更改时调用转换器,即仅一次).
这篇关于让WPF Tabcontrol高度假定最大项目的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!