使用linq查询绑定WPF Treeview

时间:2013-05-19 22:34:26

标签: wpf linq

我在SQL Server 2012中有这个4表

DepartmentsTbl
==============
DepID ------- DepName
1     ------- Human Resources
2     ------- Financial Management

SpecialTbls:
============
SpclID ------- SpclName
1     ------- Manager
2     ------- Secretary
3     ------- Data entry

EmployeesTbl:
============
EmpID ------- EmpName
1     ------- Jack
2     ------- Mark
3     ------- Sara

JobDescriptionTbls:
===================
JDID ------- EmpID ------- DepID ------- SpclID 
1    ------- 1     ------- 1     -------  1
2    ------- 2     ------- 1     -------  2
3    ------- 2     ------- 1     -------  3

注意 (有时候部门没有雇员,也必须出现在树视图中)

我想根据部门名称(例如

)在树视图中显示我的数据
DepName ------- first node
SpecialName --- Second node
EmpFullName --- Third node

我使用Linq查询来获取我的数据和XAML:

XAML:

<TreeView.ItemTemplate>
  <HierarchicalDataTemplate ItemsSource="{Binding Path=DepartmentsTbls}">
    <TextBlock Text="{Binding Path=DepName}"
               Foreground="#FFF59E13" />
    <HierarchicalDataTemplate.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding Path=SpecialName}"
                   Foreground="White" />
      </DataTemplate>
      <HierarchicalDataTemplate.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=EmpFullName}"
                     Foreground="White" />
        </DataTemplate>
      </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
</TreeView.ItemTemplate>

LINQ:

var Employees = (from spcl in menof.SpecailTbls
                         join deps in menof.DepartmentsTbls
                         on spcl.SoecialID equals deps.DepID
                         //from deps in menof.DepartmentsTbls
                         join eJD in menof.EmpJobDescriptionTbls
                         on deps.DepID equals eJD.DepID
                         join emps in menof.EmployeesTbles
                         on eJD.EmpID equals emps.EmpID
                         select new { spcl.SpecialName,deps.DepName,emps.EmpFullName }).ToList();
        tvEmpsName.ItemsSource = Employees;

但是只有第一个节点出现,我的数据才会正确显示。 所以我的问题是这里的错误在哪里。 感谢。

1 个答案:

答案 0 :(得分:0)

以下是HierarchicalDataTemplate的简短工作示例。

<强> XAML

<Window x:Class="WpfApplication1.TreeViewExample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="TreeViewExample" Height="600" Width="600"
        Loaded="Window_Loaded">
    <Grid>
        <TreeView ItemsSource="{Binding Artists}">
            <TreeView.Resources>
                <HierarchicalDataTemplate   DataType="{x:Type local:Artist}" ItemsSource="{Binding Albums}" >
                    <TextBlock Text="{Binding ArtistName}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate  DataType="{x:Type local:Album}" ItemsSource="{Binding Tracks}" >
                    <TextBlock Text="{Binding AlbumName}"/>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type local:Track}">
                    <TextBlock Text="{Binding TrackName}"/>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>

代码背后

public partial class TreeViewExample : Window, INotifyPropertyChanged {
    public TreeViewExample() {
        InitializeComponent();
        this.DataContext = this;
    }

    public List<Artist> Artists { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    //Populate Sample Data
    private void Window_Loaded(object sender, RoutedEventArgs e) {

        //First Artist node data
        List<Track> Tracks11 = new List<Track> { new Track("Track111"), new Track("Track112") };
        List<Track> Tracks12 = new List<Track> { new Track("Track121"), new Track("Track122") };
        List<Album> Albums1 = new List<Album> { new Album("Album11", Tracks11), new Album("Album12", Tracks12) };

        //Second Artist node data
        List<Track> Tracks21 = new List<Track> { new Track("Track211"), new Track("Track212") };
        List<Track> Tracks22 = new List<Track> { new Track("Track221"), new Track("Track222") };
        List<Album> Albums2 = new List<Album> { new Album("Album21", Tracks21), new Album("Album22", Tracks22) };

        Artists = new List<Artist> { new Artist("Artist1", Albums1), new Artist("Artist2", Albums2) };
        PropertyChanged(this, new PropertyChangedEventArgs("Artists"));
    }
}

public class Artist {
    public Artist(string artistName, List<Album> albums) {
        this.ArtistName = artistName;
        this.Albums = albums;
    }
    public string ArtistName { get; set; }
    public List<Album> Albums { get; set; }
}

public class Album {
    public Album(string albumName, List<Track> tracks) {
        this.AlbumName = albumName;
        this.Tracks = tracks;
    }
    public string AlbumName { get; set; }
    public List<Track> Tracks { get; set; }
}

public class Track {
    public Track(string trackName) {
        this.TrackName = trackName;
    }
    public string TrackName { get; set; }
}