【问题标题】:Expand treeview nodes展开树视图节点
【发布时间】:2015-03-25 04:09:21
【问题描述】:

我正在树视图中显示一个小模型(调查)。节点(=答案)有复选框,加载调查时,标记的答案(属性“IsSelected”= true)被成功检查。

我遇到的麻烦是在初始加载时扩展“IsSelected”= true 的那些节点。我尝试使用一种样式来完成此操作,但无法使其正常工作。例如,请参阅以下样式:

<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded"
            Value="{Binding Path=IsSelected}">
    </Setter>
</Style>
</TreeView.ItemContainerStyle>

这看起来不错(至少对我来说),但它没有效果 - 我猜 datacontext 有问题并且绑定无法掌握正确的“IsSelected” - 那应该是我也绑定的那个复选框。 如何获得正确的数据上下文并扩展这些节点?非常感谢您的帮助或提示!


为了完整起见,这里是树视图的完整 xaml:

<TreeView ItemsSource="{Binding QuestionTree}">

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:Question}" ItemsSource="{Binding Converter={StaticResource QuestionConverter}}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Options}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:GroupOption}" ItemsSource="{Binding Options}">
        <StackPanel Orientation="Horizontal">
            <CheckBox Content="{Binding Path=Name}"
                      IsChecked="{Binding Path=IsSelected}"/>
        </StackPanel>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:MainOption}" ItemsSource="{Binding MainOptions}">
        <StackPanel Orientation="Horizontal">
            <CheckBox Content="{Binding Path=Name}"
                      IsChecked="{Binding Path=IsSelected}"/>
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.Resources>

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded"
                Value="{Binding Path=IsSelected}">
        </Setter>
    </Style>
</TreeView.ItemContainerStyle>

还有我的小模型:

public class Question
{
    public string Name { get; set; }
    public List<MainOption> MainOptions { get; set; }
    public List<Group> Groups { get; set; }
}

public class MainOption
{
    public string Name { get; set; }
    public int MetaItemId { get; set; }
    public bool IsSelected { get; set; }
}

public class Group
{
    public string Name { get; set; }
    public List<GroupOption> Options { get; set; }
}

public class GroupOption
{
    public string Name { get; set; }
    public int MetaItemId { get; set; }
    public bool IsSelected { get; set; }
}   

【问题讨论】:

  • 你没有在任何地方使用 INotifyPropertyChanged...没有那个 WPF 不知道属性已经改变
  • 嗯,没有任何变化。我说的是初始显示。加载后,我希望树视图扩展所有“已检查”的项目。它适用于“已检查”属性..

标签: c# wpf


【解决方案1】:

如果您已经将TreeViewItem.IsSelected 属性数据绑定到您的数据属性,那么您可以从TreeViewItem.IsSelected 属性中设置TreeViewItem.IsExpanded 属性。试试这个:

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding Path=IsSelected, 
        RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}">
    </Setter>
</Style>

虽然这当然会将两个属性联系在一起,但您可能不希望这样做。

【讨论】:

  • 这没有效果,我希望我能告诉你原因,但我不能:o(
  • Sheridan,感谢您的评论,我猜您误解了我,因为我选择了错误的参数名称:我没有数据绑定 TreeViewItem.IsSelected-Property。到目前为止,我所做的只是将复选框的 IsChecked-Property 绑定到一个名为“IsSelected”的自定义属性。
【解决方案2】:

我终于让它工作了.. 愚蠢的我.. 我为 LEAVES 而不是 NODES 设置了“IsExpanded”.. 将属性“ContainsSelectedItems”添加到 NODE-classes Question 和 Group

    public bool ContainsSelectedItems
    {
        get
        {
            if (this.MainOptions != null && this.MainOptions.Any(x => x.IsSelected == true))
            {
                return true;
            };
            if (this.Groups != null && this.Groups.SelectMany(x => x.Options).Any(y => y.IsSelected == true))
            {
                return true;
            };
            return false;
        }
    }

并在 xaml 中使用此属性

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

有效!

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 2015-09-02
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2011-07-25
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多