gpt4 book ai didi

c# - 在 WPF 的 HierarchicalDataTemplate 中获取 TreeViewItem 的父级

转载 作者:太空狗 更新时间:2023-10-29 23:02:37 25 4
gpt4 key购买 nike

我正在合并在 Internet 上找到的两个示例。一约stretched selection styles还有一个关于 multi-selection in a treeview .

除了 TreeView 的缩进外,我一切正常。我可以提供我所有的代码,但这并不能解决问题。

我的问题出在以下方法:

public static class TreeViewItemExtensions
{
public static int GetDepth(this TreeViewItem item)
{
FrameworkElement elem = item;
while (elem.Parent != null)
{
var tvi = elem.Parent as TreeViewItem;
if (tvi != null)
return tvi.GetDepth() + 1;
elem = elem.Parent as FrameworkElement;
}
return 0;
}
}

此方法尝试检索树中 treeviewItem 的深度。问题是:elem.Parent 始终是 null。这导致深度为 0。

我认为这正在发生,因为我正在使用 HierarchicalDataTemplate。我的 xaml 的一部分看起来像这样。

<TreeView Name="folderTree"
ItemsSource="{Binding Folders}"
SelectedItemChanged="folderTree_SelectedItemChanged"
HorizontalContentAlignment="Stretch">

<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Folders}"
DataType="{x:Type Model:IFolder}">

<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}"
Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>

<Image Source="{Binding Converter={StaticResource iconConverter}}" Height="{Binding ElementName=theFile,Path=ActualHeight}"/>
<TextBlock Text="{Binding FileName}" Name="theFile"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

我的 TreeView 样式的 XAML 如下所示:

<Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">

<!-- leaving some stuff out here -->

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">

<ControlTemplate.Resources>
<local:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" />
</ControlTemplate.Resources>

<StackPanel>
<!-- The upper part of the TreeViewItem -->
<Border Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">

<!-- The margin is what we try to measure, how can we get the parents from the templatedParents? -->
<Grid Margin="{Binding Converter={StaticResource lengthConverter},
RelativeSource={RelativeSource TemplatedParent},
Path=.}">

<Grid.ColumnDefinitions>
<ColumnDefinition Width="19" />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<ToggleButton x:Name="Expander"
Style="{StaticResource ExpandCollapseToggleStyle}"
IsChecked="{Binding Path=IsExpanded,
RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>

<ContentPresenter x:Name="PART_Header"
Grid.Column="1"
ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Grid>
</Border>

<!-- the children of the TreeViewItem -->
<ItemsPresenter x:Name="ItemsHost" />
</StackPanel>

<!-- leaving some stuff out here with triggers -->

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How can I make the HierarchicalDataTemplate fill the Parent property?

最佳答案

我会改为扫描可视化树。

这是一个简单(甚至不那么优雅)的解决方案:

public static class TreeViewItemExtensions
{
public static int GetDepth(this TreeViewItem item)
{
DependencyObject target = item;
var depth = 0;
while (target != null)
{
if (target is TreeView)
return depth;
if (target is TreeViewItem)
depth++;

target = VisualTreeHelper.GetParent(target);
}
return 0;
}
}

干杯。

关于c# - 在 WPF 的 HierarchicalDataTemplate 中获取 TreeViewItem 的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6818069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com