【发布时间】:2018-04-25 19:37:58
【问题描述】:
我有一个 WPF TreeView,我想像它的父级一样将 TreeViewItem 拉伸到整个空间。
<TreeView Name="treeFamilies" AllowDrop="True" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MyNode}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Background="LightGray"/>
<TextBlock Text=" [" Foreground="Blue" Background="LightGray"/>
<TextBlock Text="{Binding Members.Count}" Foreground="Blue" Background="LightGray"/>
<TextBlock Text="]" Foreground="Blue" Background="LightGray" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
看来我错误地使用了 ItemContainer 样式 <Setter Property="HorizontalContentAlignment" Value="Stretch"/>。我现在的树看起来像这张照片:
我希望拉伸灰色背景以填充树视图控件的整个末端。 我怎样才能做到这一点?
【问题讨论】:
-
看看here
-
不确定,但我认为使用
StackPanel会给您带来一些问题。我经常无法按照我想要的方式使用它。您还可以考虑将树项的宽度、RelativeSource 绑定到父元素。 -
@Il Vic 您的链接非常有用。