WPF TreeViewItem 控件模板部分应用?

发布于 2024-10-29 16:27:02 字数 3270 浏览 3 评论 0原文

我知道 ControlTemplate 应该完全替换默认的,并且在不使用 HierarchicalDataTemplate 时这似乎可以工作。然而,使用 HierarchicalDataTemplate 我的控件模板似乎被部分使用 - TreeViewItem 是我指定的包含图像等的控件。但仍然显示为带有默认扩展器的节点以显示其子项 - 未在我的模板中指定(我想要我的孩子总是显示出来,但这不是重点)。它看起来像这样:

      <TreeView>
        <TreeView.Resources>

            <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="IsExpanded" Value="true"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem">
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                                <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" BorderBrush="{StaticResource itemBorderBrush}" Background="{StaticResource itemBackgroundBrush}" x:Name="border">
                                    <DockPanel LastChildFill="False">
                                        <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
                                            <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
                                            <Image Source="MyNode.png" Stretch="None"/>
                                            <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>                                                   
                                        </StackPanel>
                                    </DockPanel>
                                </Border>
                                <ItemsPresenter />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" IsItemsHost="True" HorizontalAlignment="Center" />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
                <TreeViewItem Style="{StaticResource MyNodeStyle}"/>
            </HierarchicalDataTemplate>

        </TreeView.Resources>

        <!-- Arrange the root items horizontally. -->
        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Center" />
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>

    </TreeView>

由于某种原因,仅当使用 HierarchicalDataTemplate 时,ItemsPanel 和 Setter 似乎并未应用,并且子项显示在默认扩展器中。 当我使用时,该扩展器是如何到达那里的我自己的 ControlTemplate!?@#$

I know a ControlTemplate is suppose to replace the default one entirely and this seems to work when not using a HierarchicalDataTemplate. However using a HierarchicalDataTemplate my control template seems to be partially used - the TreeViewItem is the control I specified containing an image etc. BUT still appears as a node with the default expander to show its children - not specified in my Template (I want my children to always be shown, but thats beside the point). It looks like this:

      <TreeView>
        <TreeView.Resources>

            <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="IsExpanded" Value="true"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem">
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                                <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" BorderBrush="{StaticResource itemBorderBrush}" Background="{StaticResource itemBackgroundBrush}" x:Name="border">
                                    <DockPanel LastChildFill="False">
                                        <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
                                            <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
                                            <Image Source="MyNode.png" Stretch="None"/>
                                            <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>                                                   
                                        </StackPanel>
                                    </DockPanel>
                                </Border>
                                <ItemsPresenter />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" IsItemsHost="True" HorizontalAlignment="Center" />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
                <TreeViewItem Style="{StaticResource MyNodeStyle}"/>
            </HierarchicalDataTemplate>

        </TreeView.Resources>

        <!-- Arrange the root items horizontally. -->
        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Center" />
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>

    </TreeView>

For some reason only when using a HierarchicalDataTemplate the ItemsPanel and Setter does not seem to be applied and the children are presented in the default expander. How did that expander get there when I am using my own ControlTemplate!?@#$

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惟欲睡 2024-11-05 16:27:02

我认为您不应该将 TreeViewItem 放入您的 HierarchicalDataTemplate 中。

试试这个:

<HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
    <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
        <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
        <Image Source="MyNode.png" Stretch="None"/>
        <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>
    </StackPanel>
</HierarchicalDataTemplate>

现在,你的模板变成:

<ControlTemplate TargetType="TreeViewItem">
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
        <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" x:Name="border">
            <DockPanel LastChildFill="False">
                <ContentPresenter ContentSource="Header" />
            </DockPanel>
        </Border>
        <ItemsPresenter />
    </StackPanel>
</ControlTemplate>

这就是你想要的样子吗?

编辑:原始扩展器可能在那里,因为您只对子项使用样式 - 将您的样式设置为树视图的 ItemContainerStyle :

<Window.Resources>
    <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
        ....

<TreeView ItemContainerStyle="{StaticResource MyNodeStyle}">

I don't think you should put the TreeViewItem inside your HierarchicalDataTemplate.

Try this:

<HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
    <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
        <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
        <Image Source="MyNode.png" Stretch="None"/>
        <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>
    </StackPanel>
</HierarchicalDataTemplate>

Now, your template becomes:

<ControlTemplate TargetType="TreeViewItem">
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
        <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" x:Name="border">
            <DockPanel LastChildFill="False">
                <ContentPresenter ContentSource="Header" />
            </DockPanel>
        </Border>
        <ItemsPresenter />
    </StackPanel>
</ControlTemplate>

Is that how you intended it to look?

Edit: the original expanders are probably there because you only use your style for child items - make your style the ItemContainerStyle for the treeview:

<Window.Resources>
    <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
        ....

<TreeView ItemContainerStyle="{StaticResource MyNodeStyle}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文