Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 展开树视图节点_C#_Wpf - Fatal编程技术网

C# 展开树视图节点

C# 展开树视图节点,c#,wpf,C#,Wpf,我正在树视图中显示一个小模型。节点=答案具有复选框,加载调查时,将成功选中带有IsSelected=true属性的标记答案 我遇到的问题是在初始加载时扩展ISSELECT=true的节点。我曾尝试使用一种风格来实现这一点,但无法使其发挥作用。例如,请参见以下样式: <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpan

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

我遇到的问题是在初始加载时扩展ISSELECT=true的节点。我曾尝试使用一种风格来实现这一点,但无法使其发挥作用。例如,请参见以下样式:

<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded"
            Value="{Binding Path=IsSelected}">
    </Setter>
</Style>
</TreeView.ItemContainerStyle>
至少在我看来,这看起来不错,但没有效果——我猜datacontext有问题,绑定无法抓住正确的IsSelected——这应该是我也要绑定复选框的地方。如何获得正确的datacontext并扩展这些节点?非常感谢您的帮助或提示

为完整起见,以下是treeview的完整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; }
}   

如果已经将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>

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

我终于让它工作了。。我真傻。。我为叶子而不是节点设置了IsExpanded。。向节点类和组添加属性时都包含SelectEdItems

    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>

工作

您没有在任何地方使用INotifyPropertyChanged。。。如果WPF不知道某个属性有changedWell,那么就根本没有更改。我说的是最初的展示。加载时,我希望树状视图将所有“选中”的项展开。它适用于选中的属性。这没有效果,我希望我能告诉你为什么,但我不能:oSheridan,谢谢你的评论,我想你误解了我,因为我选择了一个错误的参数名称:我没有数据绑定TreeViewItem.IsSelected-property。到目前为止,我所做的只是将复选框的IsChecked属性绑定到名为IsSelected的自定义属性。