[英]WPF Tabcontrol take size of largest tab
I have a TabControl that is bound to a collection of Viewmodels, which get translated into an appropriate to be drawn into the tab:我有一个绑定到 Viewmodels 集合的 TabControl,它被翻译成适当的以绘制到选项卡中:
<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" />
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.我一直试图找到一种方法来始终以任何子项的最大宽度和高度绘制 TabControl。 Let WPF Tabcontrol height assume height of largest item? 让 WPF Tabcontrol 高度假设最大项目的高度? mentions a couple of ways to achieve this in the answers (from what I udnerstood):在答案中提到了实现这一目标的几种方法(来自我所理解的):
Does anyone have any experience solving a similar issue?有没有人有解决类似问题的经验? I seem to be hitting more walls every time I make some progress on this.每次我在这方面取得一些进展时,我似乎都会撞到更多的墙。
您需要使用可以自动适应并创建与其宽度/高度的直接绑定的命名隐藏控件,例如:
Width="{Binding ActualWidth, ElementName=MainGrid}"
In case anyone else is having this problem, I solved it with the following code in the window's ContentRendered event handler:如果其他人遇到这个问题,我在窗口的 ContentRendered 事件处理程序中使用以下代码解决了它:
tabControl.SelectedIndex = 1;
UpdateLayout();
SizeToContent = SizeToContent.Manual;
tabControl.SelectedIndex = 0;
tabsControl is the name of the TabControl object. tabsControl 是 TabControl 对象的名称。 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.本质上,我们所做的是切换到具有最大高度的选项卡(在我的示例中为索引 1),更新窗口的布局,告诉窗口停止调整大小以适应其内容,然后切换回第一个选项卡。 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.当然,此解决方案假定您知道哪个选项卡的高度最大,并且您不需要窗口在初始渲染后调整其内容大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.