【问题标题】:Dynamic treeview expansion not working with HeirarchicalDataTemplate动态树视图扩展不适用于 HeirarchicalDataTemplate
【发布时间】:2018-07-25 15:53:49
【问题描述】:

我有一个树视图控件,它通过模板选择器动态选择项目控件。我已使用 Setter 将模型上的属性 IsExpanded 绑定到 TreeViewItem 上的 IsExpanded。 (我知道这是有关联的,因为如果我在模型构造函数中将 IsExpanded 设置为 true,整个树就会按预期展开。)

这就是问题所在。在我加载树之后,只有根节点可见(如预期的那样),但如果我将节点上的 IsExpanded 设置为 true,则树应该扩展到更改的节点,但事实并非如此。 (我已将调试标记放入以确保该属性实际上正在更改。)

这是我的 xaml:

<Window.Resources>
    <HierarchicalDataTemplate x:Key="RegularNodeTemplate" 
        ItemsSource="{Binding Path=Children}" >
        <StackPanel Orientation="Horizontal">
            <Border Width="8" Height="15" >
                <Label Content="*" Padding="0" HorizontalAlignment="Right" Visibility="{Binding ModifiedCueVisibility}" />
            </Border>
            <TextBlock Text="{Binding Path=ModelDisplayName}"/>
        </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="RootNodeTemplate" 
        ItemsSource="{Binding Path=Children}" >
        <Grid>
            <StackPanel Orientation="Horizontal">
                <TextBlock FontWeight="Bold" Text="{Binding Path=ModelDisplayName}"/>
            </StackPanel>
        </Grid>
    </HierarchicalDataTemplate>
    <local:ManifestNodeTemplateSelector x:Key="manifestNodeTemplateSelector"/>
</Window.Resources>
<Grid>                        
    <TreeView Name="TheManifestTreeView" Grid.Row="0" ItemsSource="{Binding ManifestRoot}" 
              ItemTemplateSelector="{StaticResource manifestNodeTemplateSelector}"
              SelectedItemChanged="TreeView_SelectedItemChanged" >
        <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded}"/>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
</Grid>

这是 IsExpanded 的代码:

    private bool _isExpanded;
    public bool IsExpanded
    {
        get => _isExpanded;
        set
        {
            _isExpanded = value;
            NotifyPropertyChanged("IsExpanded");
        }
    }

【问题讨论】:

    标签: wpf data-binding treeview


    【解决方案1】:

    我的问题是,如果该节点展开,我希望树会自动展开到叶节点。如果我希望树扩展到一个节点,我需要沿着父系向上并展开它们。

    【讨论】:

      【解决方案2】:

      似乎ItemContainerStyle只适用于TreeViewItem的第一级。尝试将样式放在TreeView.Resources中

      <TreeView.Resources>
             <Style TargetType="TreeViewItem">
                      <Setter Property="IsExpanded" Value="{Binding IsExpanded}"/>
             </Style>
      </TreeView.Resources>
      

      【讨论】:

        猜你喜欢
        • 2015-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        • 2018-02-22
        相关资源
        最近更新 更多