2013-05-19 32 views
0

我在SQL Server中有这样的4台2012绑定WPF树视图使用LINQ查询

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; 

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

+0

我不认为做HierarchicalDataTemplate是你所采用的来这里的路上。 HierarchicalDataTemplate在层次结构中显示不同类型的嵌套对象,由DataType和子项ItemsSource属性标识。您正在传递一个ItemSource并期待模板嵌套它,但是基于什么?哪些对象是哪些对象的子对象?如果您需要一个HierarchicalDataTemplate如何使用的简单工作示例,那么我可以将其粘贴到答案中。 – Jatin

+0

Ok Nirvan,你能帮我建议如何在treeview中显示这些数据。 –

+0

部门似乎位于层次结构的顶部,因此在特定部门下创建一个**部门**对象,其中包含儿童**特惠**作为列表。 **特殊**对象将有一个**雇员**名单。所有这些都可以使用LINQ to SQL查询完成(http://msdn.microsoft.com/zh-CN/library/bb386932.aspx)。一旦你拥有包含分层数据的Department对象设置,就可以使用HierarchicalDataTemplate。我将粘贴一个HierarchicalDataTemplate如何用于回答的工作示例。 – Jatin

回答

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; } 
} 
+0

感谢Nirvan,我感谢你的帮助。 –