2017-05-15 2 views
-1

Ich habe eine ObservableCollection der KlasseDatenbindung in Data Grid mit Collection

Public class Object 
{ 
    public string Name; 
    public Employee Employee; 
} 

public class Employee 
{ 
    // few properties 
} 

hier für Collection meine xMAL Code:

<CollectionViewSource x:Key="cvsTasks" 
          Source="{Binding Reels}"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="Name" /> 
     </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 

Hier mein Datagrid-Code:

<DataGrid ItemsSource="{Binding Source={StaticResource cvsTasks}}"/> 

Jetzt CollectionViewSource mit PropertyGroupDescription auf Name und ich möchte mein DataGrid mit Employee Pro binden Teil von CollectionViewSource.

+2

Können Sie den Code schreiben Sie hier diskutieren. 'ObservableCollection',' CollectionViewSource'. Auch Klassenname ist 'Objekt'? –

+0

@MohitSinghBora: Können Sie zeigen, was "Reels" ist? Was ist sein Typ? –

+0

ObservableCollection des Objekts wird in ViewModel als "Reels" bezeichnet –

Antwort

0

Ich weiß nicht, ob ich Ihre Frage richtig verstehe.

Ausblick:

<Window.Resources> 
    <CollectionViewSource x:Key="cvsTasks" Source="{Binding List}"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="Name" /> 
     </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 
</Window.Resources> 
<Grid > 
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource cvsTasks}}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
      <DataGridTextColumn Header="EmpID" Binding="{Binding Employee.ID}"/> 
      <DataGridTextColumn Header="EmpDeportment" Binding="{Binding Employee.Department}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

Ansichtsmodell:

public class ViewModel { 
    public System.Collections.ObjectModel.ObservableCollection<Model.Root> List { get; set; } 

    public ViewModel() { 
     List = new System.Collections.ObjectModel.ObservableCollection<Model.Root> { 
      new Model.Root { 
       Name = "Peter", 
       Employee = new Model.Employee { 
        ID = 1, 
        Department = "IT" 
       } 
      }, 
      new Model.Root { 
       Name = "Hans", 
       Employee = new Model.Employee { 
        ID = 2, 
        Department = "accounting" 
       } 
      }, 
      new Model.Root { 
       Name = "Bilbo", 
       Employee = new Model.Employee { 
        ID = 3, 
        Department = "ceo" 
       } 
      } 
     }; 
    } 
} 

Modell:

public class Model { 

     public class Employee { 
      public int ID { get; set; } 
      public string Department { get; set; } 
     } 
     public class Root { 
      public string Name { get; set; } 
      public Employee Employee { get; set; } 
     } 
    } 

Und das Ergebnis: enter image description here