Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.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# WPF树视图没有显示任何内容_C#_Wpf_Binding_Treeview_Hierarchicaldatatemplate - Fatal编程技术网

C# WPF树视图没有显示任何内容

C# WPF树视图没有显示任何内容,c#,wpf,binding,treeview,hierarchicaldatatemplate,C#,Wpf,Binding,Treeview,Hierarchicaldatatemplate,我无法使数据显示在树视图中。我在树状视图上方有一个标签,它绑定到我的viewmodel,并且可以正常工作。我对树视图做了什么错误 这就是我所做的 型号: public class ViewConfiguration { public string Name { get; set; } public List<ViewRuleSet> RuleSets { get; set; } public ViewConfiguration(string name, pa

我无法使数据显示在树视图中。我在树状视图上方有一个标签,它绑定到我的viewmodel,并且可以正常工作。我对树视图做了什么错误

这就是我所做的

型号:

public class ViewConfiguration
{
    public string Name { get; set; }
    public List<ViewRuleSet> RuleSets { get; set; }

    public ViewConfiguration(string name, params ViewRuleSet[] ruleSets)
    {
        this.Name = name;
        this.RuleSets = new List<ViewRuleSet>(ruleSets);
    }
}

public class ViewRuleSet
{
    public string Name { get; set; }
    public List<ViewRule> Rules { get; set; }

    public ViewRuleSet(string name, params ViewRule[] rules)
    {
        this.Name = name;
        this.Rules = new List<ViewRule>(rules);
    }
}

public class ViewRule
{
    public string Name { get; set; }

    public ViewRule(string name)
    {
        this.Name = name;
    }
}
视图:

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm=“clr命名空间:TreeViewer.ViewModel”
xmlns:m=“clr命名空间:TreeViewer.Model”
x:Class=“TreeViewer.main窗口”
Title=“MainWindow”Height=“350”Width=“525”>

下面是我看到的:(我还尝试通过代码显式设置主窗口的DataContext。
this.DataContext=new MainViewModel();


TreeView.ItemsSource的值必须是集合,而不是单个对象。您应该绑定到
Configuration.RuleSets
,而不是
Configuration
。当然,您必须相应地调整您的
HierarchycalDataTemplate
。谢谢-我刚刚将ViewModel中的配置更改为
List Configurations
,并且成功了。您可以添加他实现IList并返回实现它以适应规则
public class MainViewModel
{
    public ViewConfiguration Configuration { get; set; }

    public MainViewModel()
    {
        var rule = new ViewRule("MyRule");

        var ruleSet = new ViewRuleSet("MyRuleSet", rule);

        this.Configuration = new ViewConfiguration("MyConfiguration", ruleSet);
    }
}
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:TreeViewer.ViewModel" 
    xmlns:m="clr-namespace:TreeViewer.Model"
    x:Class="TreeViewer.MainWindow"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

<Grid>
    <Label Margin="10,10,10,0" VerticalAlignment="Top" Content="{Binding Configuration.Name}"/>
    <TreeView ItemsSource="{Binding Configuration}" Margin="0,41,0,0">

        <!-- Configuration template -->
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding RuleSets}">
                <TextBlock Foreground="Red" Text="{Binding Name}" />

                <!-- RuleSet template -->
                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Rules}">
                        <TextBlock Text="{Binding Name}" />

                        <!-- Rule template -->
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" />
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>

            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

    </TreeView>
</Grid>