HierarchicalDataTemplate和GridSplitter

时间:2016-12-04 21:19:15

标签: c# .net wpf treeview hierarchicaldatatemplate

TreeView' HierarchicalDataTemplate似乎与GridSplitter无关。我不确定实际问题在哪里,但我怀疑ScrollViewer

当前行为: TreeView仅根据其项目对其进行调整。当更宽的项目扩展时,TreeView会增长。它不能通过网格分割器缩小尺寸或放大尺寸。 Horizo​​ntalAlignment = Stretch和ScrollViewer属性似乎不会影响这一点。

预期行为:TreeView会将宽度调整为GridSplitter,并在太小时显示水平滚动条。

如屏幕截图所示,TreeView保持其项目的最小宽度,并且不与GridSplitter对齐。

screenshot

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" MinWidth="250" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TreeView Name="treMain" BorderThickness="0" ItemsSource="{Binding TreeViewSections}" ScrollViewer.HorizontalScrollBarVisibility="Visible">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewEntry}" ItemsSource="{Binding Items}">
            <StackPanel Orientation="Horizontal" Margin="5,3">
                <Image Margin="0,0,5,0">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Setter Property="Source" Value="{Binding Icon}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
                                    <Setter Property="Source" Value="{qc:Binding '$P.ExpandedIcon == null || $P.Items.Count == 0 ? $P.Icon : $P.ExpandedIcon', P={Binding}}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
                <TextBlock Text="{Binding Text}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
            <EventSetter Event="MouseDoubleClick" Handler="treMain_MouseDoubleClick" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

1 个答案:

答案 0 :(得分:1)

问题是GridSplitter的ResizeBehavior。使用ResizeBehavior="PreviousAndNext"

可以实现预期的行为
<GridSplitter Grid.Column="1" ResizeBehavior="PreviousAndNext" Width="4"/>