具有固定宽度列的WPF Treeview

8 wpf treeview listview

我想创建一个树视图和列表视图的混合.我想要2列.在左栏中,我想要一个递归树视图,右栏应显示左栏中有关项目的一些信息.我们调用左列Name和右列Value.问题是,当您展开树视图时,缩进级别会更改,并且"值"列将变为未对齐.

我能想出的就是:

答:使用内置的TreeView并根据缩进级别手动更改Name-column的宽度,以便value-column始终对齐.

B.使用内置的ListView并通过在父项之间添加子项来手动创建TreeView,并更改这些项的缩进.

真的没有更好的方法吗?

Den*_*ler 6

有一种方法,我在Silverlight应用程序中有这样的野兽

您需要调整treeviewitem的模板.默认模板不会一直延伸到树视图中.

通过tweeking模板,您可以让它一直扩展,然后您可以将DataTemplate(或HierarchicalDataTemplate)设置为网格.如果我没记错的话,你需要得到TreeviewItem的默认模板的副本,并将"Header"元素的Horizo​​ntalAlignment属性更改为"Stretch",删除构成模板的网格中最右边的列,然后更改包含从"Auto"到"*"的元素的列的宽度.

使用混合物很容易.创建一个TreeViewItem,右键单击它并选择"编辑控件("模板")>编辑副本..."这将创建TreeViewItem的默认模板的副本.从那里,找到名为PART_Header的ContentPresenter.从这里走,找到包含网格并修改其列以匹配我的解释(删除最后一列,将第二列从"自动"更改为"*").在为项目创建的样式中,将Horizo​​ntalContentAlignment设置为"Stretch"(默认情况下绑定到其他内容).

在树视图中使用作为ItemContainerStyle创建的样式.您可以先删除之后创建的TreeViewItem.

最后,你最终会得到一堆资源,其中一个就是你的风格.请参阅下面的内容(TreeViewItem样式和具有名称和值列的项目的基本DataTemplate).TreeViewItem样式/模板引用创建了其他资源,但这里没有显示它们(因为它已经太长了:p).

<Window.Resources>    
<Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="1,0,0,0"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MinWidth="19" Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ClickMode="Press" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <Border x:Name="Bd" SnapsToDevicePixels="true" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header"/>
                        </Border>
                        <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="false">
                            <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="HasItems" Value="false">
                            <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

        <HierarchicalDataTemplate x:Key="DataTemplate1"                                   
                                  ItemsSource="{Binding SubNodes}">
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock HorizontalAlignment="Left"
                           VerticalAlignment="Top"
                           Grid.Column="1"
                           Text="HEADER"
                           TextWrapping="Wrap" />
                <TextBox HorizontalAlignment="Left"
                         Margin="2"
                         VerticalAlignment="Top"
                         Text="VALUE"
                         TextWrapping="Wrap"
                         Grid.Column="2" />
            </Grid>

</Window.Resources>

<TreeView HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  Width="Auto"
                  Height="Auto"
                  x:Name="trv"
          ItemContainerStyle="{StaticResource TreeViewItemStyle1}"
                  ItemTemplate="{DynamicResource DataTemplate1}">            
        </TreeView>
Run Code Online (Sandbox Code Playgroud)

请注意,您需要确保包含右侧单元格的网格列始终具有相同的宽度,否则您会有一些奇怪的东西(我使用具有固定宽度内容的"自动"列,并添加一个空白"*"名称和"单元格"之间的列,以右对齐它们.

另请注意,此解决方案基本上将树视图的外观"冻结"为您计算机上的任何主题.(根据您在进行修改时使用的操作系统,在Vista和XP机器上看起来会一样).