0

我在 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>

林克:

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;

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

4

1 回答 1

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; }
}
于 2013-05-20T10:26:42.677 回答