WPF Tabcontrol take size of largest tab

痴心易碎 提交于 2019-12-10 04:12:09

问题


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" />

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):

  • using Grid with SharedSizeGroup - seems to need to be applied to a DataTemplate on the actual content of the TabControl, which overwrites the automated View drawing achieveing by the VM->View mapping created in the resourcedictionary
  • Using a width/height converter, which would require my TabControl to be bound to a collection of UI elements instead of viewmodels

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


回答1:


You need to use named hidden controls that can fit automatically and create straight bind to their width/height like:

 Width="{Binding ActualWidth, ElementName=MainGrid}"


来源:https://stackoverflow.com/questions/31267946/wpf-tabcontrol-take-size-of-largest-tab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!