WPF Tabcontrol 采用最大选项卡的大小 [英] WPF Tabcontrol take size of largest tab

查看:56
本文介绍了WPF Tabcontrol 采用最大选项卡的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到一组 Viewmodel 的 TabControl,它被翻译成一个合适的以绘制到选项卡中:

I have a TabControl that is bound to a collection of Viewmodels, which get translated into an appropriate to be drawn into the tab:

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type charting:LineFormatViewModel}">
            <charting:LineFormatView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type charting:IndexSettingsViewModel}">
            <charting:IndexSettingsView />
        </DataTemplate>
        ....
    </ResourceDictionary>
</Window.Resources>

<TabControl ItemSource="Binding ViewModels" />

我一直在努力寻找一种方法,始终以任何子项的最大宽度和高度绘制 TabControl.让 WPF Tabcontrol 高度假设最大项目的高度?在答案中提到了实现这一目标的几种方法(来自我的 udnerstood):

I've been trying to find a way to always draw the TabControl at the maximum width and height of any of it's children. Let WPF Tabcontrol height assume height of largest item? mentions a couple of ways to achieve this in the answers (from what I udnerstood):

  • 使用带有 SharedSizeGroup 的 Grid - 似乎需要应用到 TabControl 实际内容上的 DataTemplate,它会覆盖 VM-> 在资源字典中创建的视图映射实现的自动化视图绘制
  • 使用宽度/高度转换器,这将要求我的 TabControl 绑定到 UI 元素的集合而不是视图模型

有没有人有解决类似问题的经验?每次我在这方面取得一些进展时,我似乎都会遇到更多的障碍.

Does anyone have any experience solving a similar issue? I seem to be hitting more walls every time I make some progress on this.

推荐答案

如果其他人遇到这个问题,我在窗口的 ContentRendered 事件处理程序中使用以下代码解决了它:

In case anyone else is having this problem, I solved it with the following code in the window's ContentRendered event handler:

tabControl.SelectedIndex = 1;
UpdateLayout();
SizeToContent = SizeToContent.Manual;
tabControl.SelectedIndex = 0;

tabsControl 是 TabControl 对象的名称.本质上,我们所做的是切换到具有最大高度的选项卡(在我的示例中为索引 1),更新窗口的布局,告诉窗口停止调整大小以适应其内容,然后切换回第一个选项卡.当然,此解决方案假设您知道哪个选项卡的高度最大,并且您不需要窗口在初始渲染后调整其内容大小.

tabsControl is the name of the TabControl object. Essentially what we do is switch to the tab with the greatest height (at index 1 in my example), update the window's layout, tell the window to stop resizing to fit its contents, and then switch back to the first tab. Of course, this solution assumes you know which tab has the greatest height and that you won't need the window to resize to its contents after the initial render.

这篇关于WPF Tabcontrol 采用最大选项卡的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆