【问题标题】:WPF UserControl TreeView HeightWPF 用户控件树视图高度
【发布时间】:2010-11-25 19:54:32
【问题描述】:

我有一个包含 StackPanel 和 TreeView 的用户控件。 所有控件都有 Height="Auto"

当我在窗口上使用自定义控件并设置高度时,例如 Height=800 Stack Panel 增长到这个高度,但是 TreeView 不会自动调整高度。

用户控制:

<UserControl x:Class="WPFDataBinding.ucCompanyTreeView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="200" Width="300" Loaded="UserControl_Loaded">
<StackPanel>
    <TextBlock Background="#505050" Foreground="Gold">Companys</TextBlock>
    <TreeView Name="myTreeView" ItemTemplate="{StaticResource DetailTemplate}">
    </TreeView>
</StackPanel>

Window1.xaml:

<StackPanel Orientation="Horizontal">
    <local:ucCompanyTreeView Width="400" Height="600">
    </local:ucCompanyTreeView>
</StackPanel>

用户控件内的堆栈面板的高度会增加,但树视图不会。 我试过将树视图放在网格中,同样 到处设置 Height="Auto" 一样 到处设置 VerticalAlignment="Strech" 一样

Treeview 是数据绑定的,所以我认为它是在绑定数据后自动调整大小,但删除此数据绑定结果相同。

我可以通过调整事件大小来做到这一点....但我以前遇到过这个问题,只是想了解“某些”控件的高度继承背后的逻辑。

【问题讨论】:

    标签: wpf user-controls height


    【解决方案1】:

    如果您将用户控件中的 StackPanel 替换为 DockPanel,TreeView 将默认填充 DockPanel...

    <DockPanel>
        <TextBlock DockPanel.Dock="Top" Background="#505050" Foreground="Gold">Companys</TextBlock>
        <TreeView Name="myTreeView" ItemTemplate="{StaticResource DetailTemplate}">
        </TreeView>
    </DockPanel>
    

    How to: Choose Between StackPanel and DockPanel

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 2022-07-22
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多