多级WPF树

时间:2018-01-03 09:28:22

标签: sql wpf hierarchical-data

我有这个表:

iAccess:
-----------------
Id 
UserRef 
GroupRef    
ActionRef   
HasAccess   
HasDetail

iAction
---------------------
Id
WindowsRef
ActionName
ActionPName
DisplayIndex
CanHasDetail

iWindow
-------------------
Id
OwnerRef
WinName
WinPName
DisplayIndex

iUser
------------------------
Id
GroupRef
LoginName
Password
IsEnable
HasFullAccess
GenerationDate
Image

Database Diagram

如何在WPF Treeview中显示以下内容

预期结果:

-LoginName1
    -WinName1
        -ActionName1
        -ActionName2
     -WinName2
        -ActionName1

1 个答案:

答案 0 :(得分:-2)

您可以使用HierarchicalDataTemplate

 <TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">  
        <TreeView.ItemTemplate>  
            <HierarchicalDataTemplate ItemsSource="{Binding Positions}" DataType="{x:Type VM:Department}">  
                <Label Content="{Binding DepartmentName}"/>  
                <HierarchicalDataTemplate.ItemTemplate>  
                    <HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type VM:Position}">  
                        <Label Content="{Binding PositionName}"/>  
                        <HierarchicalDataTemplate.ItemTemplate>  
                            <DataTemplate DataType="{x:Type VM:Employee}">  
                                <Label Content="{Binding EmployeeName}"/>  
                            </DataTemplate>  
                        </HierarchicalDataTemplate.ItemTemplate>  
                    </HierarchicalDataTemplate>  
                </HierarchicalDataTemplate.ItemTemplate>  
            </HierarchicalDataTemplate>  
        </TreeView.ItemTemplate>  
    </TreeView>  

职位等级:

public class Position : ViewModelBase  
{  
    private List<Employee> employees;  

    public Position(string positionname)  
    {  
        PositionName = positionname;  
        employees = new List<Employee>()  
        {  
            new Employee("Employee1"),  
            new Employee("Employee2"),  
            new Employee("Employee3")  
        };  
    }  

    public List<Employee> Employees  
    {  
        get  
        {  
            return employees;  
        }  
        set  
        {  
            employees = value;  
            OnPropertyChanged("Employees");  
        }  
    }  

    public string PositionName  
    {  
        get;  
        set;  
    }  
}  

系类

public class Department : ViewModelBase  
{  
    private List<Position> positions;  

    public Department(string depname)  
    {  
        DepartmentName = depname;  
        positions = new List<Position>()  
        {  
            new Position("TL"),  
            new Position("PM")  
        };  
    }  
    public List<Position> Positions  
    {  
        get  
        {  
            return positions;  
        }  
        set  
        {  
            positions = value;  
            OnPropertyChanged("Positions");  
        }  
    }  
    public string DepartmentName  
    {  
        get;  
        set;  
    }  
}  

MainViewModel类:

public class MainWindowViewModel : ViewModelBase  
{  
    private List<Department> departments;  

    public MainWindowViewModel()  
    {  
        Departments = new List<Department>()  
        {  
            new Department("DotNet"),  
            new Department("PHP")  
        };  
    }  

    public List<Department> Departments  
    {  
        get  
        {  
            return departments;  
        }  
        set  
        {  
            departments = value;  
            OnPropertyChanged("Departments");  
        }  
    }  
}  

您可以阅读http://www.c-sharpcorner.com/article/populating-hierarchical-data-in-treeview-in-wpf-using-mvvm/

中的完整文章