Benutzer mit den meisten Antworten
DataGrid WPF Aufteilung

Frage
-
Hallo
Ich möchte mein DataGrid anders aufstellen. Hier ein Beispiel
z.b. für CD's
Ich möchte bei der ersten Tabelle ein [+] Zeichen wo man es aufklappen kann mit diesen Inhalt
CD-Nummer | CD-Name | CD-Anzahl
wenn man auf den [+] klickt soll es aufgeklappt werden und den Inhalt
Tittel-Nummer | Sänger | Lied Name
Wenn es 2 CD's gibt soll es noch Gruppiert werden von CD1 und CD2
Ist das mit DataGrid möglich?
Kann mir jemand ein Beispiel Code geben.
Gruß
Matthias
Antworten
-
Hi Matthias,
nachfolgend eine Demo mit dem ListView und Expander für die Details:XAML:
<Window x:Class="Window43" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window43" Height="300" Width="300"> <Window.Resources> <local:Window43VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <ListView ItemsSource="{Binding View}"> <ListView.ItemTemplate> <DataTemplate> <Border> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Column="0" Content="{Binding CD}"/> <Expander Grid.Column="1"> <DataGrid ItemsSource="{Binding Titel}"/> </Expander> </Grid> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Window>
Dazu der ViewModel:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window43VM Public Sub New() Dim col As New ObservableCollection(Of Window43CD) For i = 1 To 10 Dim cd = New Window43CD With {.CD = $"CD {i}"} col.Add(cd) For k = 1 To 9 cd.Titel.Add(New Window43Titel With {.Name = $"Titel {k}"}) Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View End Get End Property End Class Public Class Window43CD Public Property CD As String Public Property Titel As New ObservableCollection(Of Window43Titel) End Class Public Class Window43Titel Public Property Name As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:12
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:24
-
Hi Matthias,
es geht auch mit dem DataGrid und Row-Details. Dazu eine Demo:XAML:
<Window x:Class="Window44" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window44" Height="300" Width="300"> <Window.Resources> <local:Window44VM x:Key="vm"/> <Style x:Key="GroupStyle" TargetType="GroupItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GroupItem"> <StackPanel> <Label Content="Projekt"/> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="CD" Binding="{Binding CD}"/> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding TitelListe}"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> </Grid> </Window>
Und der ViewModel dazu:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window44VM Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window44CD) For i = 1 To 10 Dim cd = New Window44CD With {.CD = $"CD {i}"} col.Add(cd) For k = 1 To 9 cd.Titel.Add(New Window44Titel With {.Projekt = $"Projekt {rnd.Next(1, 4)}", .Name = $"Titel {k}"}) Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View End Get End Property End Class Public Class Window44CD Public Property CD As String Public Property Titel As New ObservableCollection(Of Window44Titel) Public ReadOnly Property TitelListe As ICollectionView Get Dim cvs As New CollectionViewSource cvs.Source = Titel cvs.View.GroupDescriptions.Add(New PropertyGroupDescription("Projekt")) Return cvs.View End Get End Property End Class Public Class Window44Titel Public Property Projekt As String Public Property Name As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:12
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:25
-
Hi Matthias,
nachfolgend eine Lösung entsprechend Deinen Vorstellungen nur mit TreeView:<Window x:Class="Window46" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window46" Height="300" Width="300"> <Window.Resources> <local:Window46VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <TreeView ItemsSource="{Binding View}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Window46CD}" ItemsSource="{Binding CDTitel}"> <StackPanel Orientation="Horizontal"> <Label Content="CD-Nummer:"/> <Label Content="{Binding Nummer}"/> <Label Content="| CD-Name:"/> <Label Content="{Binding Name}"/> <Label Content="| CD-Anzahl:"/> <Label Content="{Binding CDTitel.Count}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window46CDNummer}" ItemsSource="{Binding TitelListe}"> <StackPanel Orientation="Horizontal"> <Label Content="CD-FolgeNummer:"/> <Label Content="{Binding CDNummer}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window46Titel}"> <StackPanel Orientation="Horizontal"> <Label Content="Titel-Nummer:"/> <Label Content="{Binding TitelNummer}"/> <Label Content="| Sänger :"/> <Label Content="{Binding Saenger}"/> <Label Content="| Lied Name:"/> <Label Content="{Binding Liedname}"/> </StackPanel> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> </Window>
Dazu der ViewModel:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window46VM Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window46CD) For i = 1 To 10 Dim cd = New Window46CD With {.Nummer = i, .Name = $"Name {i}"} col.Add(cd) Dim cds = rnd.Next(1, 3) ' 1 oder mehrere CD's For k = 1 To cds Dim titelliste As New List(Of Window46CDNummer) For l = 1 To rnd.Next(3, 9) titelliste.Add(New Window46Titel _ With {.CDNummer = k, .TitelNummer = l, .Saenger = $"Sänger {rnd.Next(1, 100)}", .Liedname = $"Lied {l}"}) Next If cds = 1 Then cd.CDTitel = titelliste Else If cd.CDTitel Is Nothing Then cd.CDTitel = New List(Of Window46CDNummer) cd.CDTitel.Add(New Window46CDNummer With {.CDNummer = k, .TitelListe = titelliste}) End If Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View ' Sicht auf Liste mit CD's Angaben End Get End Property End Class Public Class Window46CD Public Property Nummer As Integer Public Property Name As String Public Property CDTitel As List(Of Window46CDNummer) End Class Public Class Window46CDNummer Public Property CDNummer As Integer Public Property TitelListe As List(Of Window46CDNummer) End Class Public Class Window46Titel Inherits Window46CDNummer Public Property TitelNummer As Integer Public Property Saenger As String Public Property Liedname As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:11
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:25
Alle Antworten
-
Hi Matthias,
alternativ zum DataGrid kannst Du auch ein ListView, LKistBox oder anderes Steuerelement nutzen. Wenn Du das ItemsTemplate entsprechend aufbaust, kannst Du darin z.B. einen Expander für Details nutzen. Der Nachteil von Row-Details im DataGrid ist, dass immer nur die Details des aktuell selektierten Datensatzes aufgeblendet werden und dies nicht gesteuert werden kann. Wenn das Dich nicht stört, dann kann das DataGrid optimal sein.--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks -
Bevor ich mit ListView anfange möchte ich aber das WPF DataGrid schon gern vorher noch Lernen
Ich möchte das DataGrid so gestalten wie auf diesen Bild
und CD1 und CD2 möchte ich noch zusätzlich so Zusammen stellen
Ist das mit dem Standard DataGrid überhaupt möche?
Zuerst möchte ich es so wie auf dem ersten Bild
Wie macht man sowas?
Gruß
Matthias
-
Hi Matthias,
nachfolgend eine Demo mit dem ListView und Expander für die Details:XAML:
<Window x:Class="Window43" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window43" Height="300" Width="300"> <Window.Resources> <local:Window43VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <ListView ItemsSource="{Binding View}"> <ListView.ItemTemplate> <DataTemplate> <Border> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Column="0" Content="{Binding CD}"/> <Expander Grid.Column="1"> <DataGrid ItemsSource="{Binding Titel}"/> </Expander> </Grid> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid> </Window>
Dazu der ViewModel:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window43VM Public Sub New() Dim col As New ObservableCollection(Of Window43CD) For i = 1 To 10 Dim cd = New Window43CD With {.CD = $"CD {i}"} col.Add(cd) For k = 1 To 9 cd.Titel.Add(New Window43Titel With {.Name = $"Titel {k}"}) Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View End Get End Property End Class Public Class Window43CD Public Property CD As String Public Property Titel As New ObservableCollection(Of Window43Titel) End Class Public Class Window43Titel Public Property Name As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:12
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:24
-
Hi Matthias,
es geht auch mit dem DataGrid und Row-Details. Dazu eine Demo:XAML:
<Window x:Class="Window44" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window44" Height="300" Width="300"> <Window.Resources> <local:Window44VM x:Key="vm"/> <Style x:Key="GroupStyle" TargetType="GroupItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GroupItem"> <StackPanel> <Label Content="Projekt"/> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="CD" Binding="{Binding CD}"/> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding TitelListe}"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> </Grid> </Window>
Und der ViewModel dazu:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window44VM Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window44CD) For i = 1 To 10 Dim cd = New Window44CD With {.CD = $"CD {i}"} col.Add(cd) For k = 1 To 9 cd.Titel.Add(New Window44Titel With {.Projekt = $"Projekt {rnd.Next(1, 4)}", .Name = $"Titel {k}"}) Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View End Get End Property End Class Public Class Window44CD Public Property CD As String Public Property Titel As New ObservableCollection(Of Window44Titel) Public ReadOnly Property TitelListe As ICollectionView Get Dim cvs As New CollectionViewSource cvs.Source = Titel cvs.View.GroupDescriptions.Add(New PropertyGroupDescription("Projekt")) Return cvs.View End Get End Property End Class Public Class Window44Titel Public Property Projekt As String Public Property Name As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:12
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:25
-
Hi Matthias,
wenn Du ein Pluszeichen haben willst, dann wäre auch ein TreeView eine optimale Möglichkeit für die Darstellung.--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks -
Hi Matthias,
mit ist nicht bekannt, dass das WPF DataGrid die Darstellung von "+" / "-" bei hierarchischen Daten unterstützt, wie das beispielsweise das klassische Windows Forms DataGrid mit in Beziehung stehenden DataTables eines DataSets ermöglicht.Wenn Du das genau so haben willst, dann programmiere das selbst aus oder nutze eine Kombination von TreeView und DataGrid.
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks -
Hallo
Das ist eine Gute idee mit TreeView und wenn man es aufklappt erscheint das DataGrid das müsste dich mit WPF möglich sein.
Hätten Sie mir für TreeView auch ein Demo?
Ist es in TreeView auch möglich das man da Spalten macht, wenn ja wie? ich hätte gerne 3 Spalten
Ich hab das mit DataGrid probiert und es sollte so gehen.
<TreeView x:Name="treeView"> <TreeViewItem Header="Test"> <DataGrid Width="190"> </DataGrid> </TreeViewItem> </TreeView>
Nur fehlen mir in TreeView die 3 Spalten noch
Gruß
Matthias
- Bearbeitet Mattes80 Sonntag, 1. Januar 2017 13:26
-
Hi Matthias,
hier mal ein Beispiel mit TreeView zum Aufklappen. Beim Aufklappen wird ein DataGrid angezeigt, in welchem die Detailsätze gruppiert dargestellt werden:XAML:
<Window x:Class="Window45" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window45" Height="300" Width="300"> <Window.Resources> <local:Window45VM x:Key="vm"/> <Style x:Key="GroupStyle" TargetType="GroupItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GroupItem"> <StackPanel> <Label Content="Projekt"/> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <TreeView ItemsSource="{Binding View}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Window45CD}" ItemsSource="{Binding TitelObjekt}"> <StackPanel> <TextBlock Text="{Binding CD}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window45ListenObjekt}"> <DataGrid ItemsSource="{Binding TitelListe}" AutoGenerateColumns="True" IsReadOnly="True"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> </Window>
Dazu der ViewModel:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window45VM Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window45CD) For i = 1 To 10 Dim cd = New Window45CD With {.CD = $"CD {i}"} col.Add(cd) Dim titobj As New Window45ListenObjekt cd.TitelObjekt.Add(titobj) For k = 1 To 9 titobj.Titels.Add(New Window45Titel With {.Projekt = $"Projekt {rnd.Next(1, 4)}", .Name = $"Titel {k}"}) Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View ' Sicht auf Liste mit CD's Angaben End Get End Property End Class Public Class Window45CD Public Property CD As String Public Property TitelObjekt As New List(Of Window45ListenObjekt) End Class
Public Class Window45ListenObjekt Public Property Projekt As String Public Property Titels As New ObservableCollection(Of Window45Titel) Public ReadOnly Property TitelListe As ICollectionView Get Dim cvs As New CollectionViewSource cvs.Source = Titels cvs.View.GroupDescriptions.Add(New PropertyGroupDescription("Projekt")) Return cvs.View End Get End Property End Class Public Class Window45Titel Public Property Projekt As String Public Property Name As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Bearbeitet Peter Fleischer Sonntag, 1. Januar 2017 17:10
-
Hi Matthias,
nachfolgend eine Lösung entsprechend Deinen Vorstellungen nur mit TreeView:<Window x:Class="Window46" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1VB" mc:Ignorable="d" Title="Window46" Height="300" Width="300"> <Window.Resources> <local:Window46VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <TreeView ItemsSource="{Binding View}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Window46CD}" ItemsSource="{Binding CDTitel}"> <StackPanel Orientation="Horizontal"> <Label Content="CD-Nummer:"/> <Label Content="{Binding Nummer}"/> <Label Content="| CD-Name:"/> <Label Content="{Binding Name}"/> <Label Content="| CD-Anzahl:"/> <Label Content="{Binding CDTitel.Count}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window46CDNummer}" ItemsSource="{Binding TitelListe}"> <StackPanel Orientation="Horizontal"> <Label Content="CD-FolgeNummer:"/> <Label Content="{Binding CDNummer}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window46Titel}"> <StackPanel Orientation="Horizontal"> <Label Content="Titel-Nummer:"/> <Label Content="{Binding TitelNummer}"/> <Label Content="| Sänger :"/> <Label Content="{Binding Saenger}"/> <Label Content="| Lied Name:"/> <Label Content="{Binding Liedname}"/> </StackPanel> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> </Window>
Dazu der ViewModel:
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class Window46VM Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window46CD) For i = 1 To 10 Dim cd = New Window46CD With {.Nummer = i, .Name = $"Name {i}"} col.Add(cd) Dim cds = rnd.Next(1, 3) ' 1 oder mehrere CD's For k = 1 To cds Dim titelliste As New List(Of Window46CDNummer) For l = 1 To rnd.Next(3, 9) titelliste.Add(New Window46Titel _ With {.CDNummer = k, .TitelNummer = l, .Saenger = $"Sänger {rnd.Next(1, 100)}", .Liedname = $"Lied {l}"}) Next If cds = 1 Then cd.CDTitel = titelliste Else If cd.CDTitel Is Nothing Then cd.CDTitel = New List(Of Window46CDNummer) cd.CDTitel.Add(New Window46CDNummer With {.CDNummer = k, .TitelListe = titelliste}) End If Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View ' Sicht auf Liste mit CD's Angaben End Get End Property End Class Public Class Window46CD Public Property Nummer As Integer Public Property Name As String Public Property CDTitel As List(Of Window46CDNummer) End Class Public Class Window46CDNummer Public Property CDNummer As Integer Public Property TitelListe As List(Of Window46CDNummer) End Class Public Class Window46Titel Inherits Window46CDNummer Public Property TitelNummer As Integer Public Property Saenger As String Public Property Liedname As String End Class
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort vorgeschlagen Peter Fleischer Sonntag, 1. Januar 2017 19:11
- Als Antwort markiert Dimitar DenkovMicrosoft contingent staff, Moderator Dienstag, 11. April 2017 14:25
-
Hi Matthias,
die Demo ist nur ein Fenster und ein ViewModel. Beide Teile kannst Du einfach in ein Projekt kopieren, z.B. als Window46.Xaml und Window46VM.vb. In der App.Xaml setzt Du Window46.xaml als Start-Uri. Ich arbeite mit VS2015.Ansonsten, wenn das bei Dir nicht läuft, solltest Du vielleichrt mal den PC einschalten :-) oder zumindest etwas genauer schreiben, was Du gemacht hast und welche Fehler angezeigt werden.
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Bearbeitet Peter Fleischer Dienstag, 3. Januar 2017 13:49
-
Ich hab es so gemacht
Bei mir wird aber nur eine Weiße Seite angezeigt ohne Inhalt
<Window x:Class="F_Datagrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Test_Programm" mc:Ignorable="d" Title="F_Datagrid" Height="300" Width="300"> <Window.Resources> <local:F_Datagrid x:Key="vm"/> <Style x:Key="GroupStyle" TargetType="GroupItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GroupItem"> <StackPanel> <Label Content="Projekt"/> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <TreeView ItemsSource="{Binding View}"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Window45CD}" ItemsSource="{Binding TitelObjekt}"> <StackPanel> <TextBlock Text="{Binding CD}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Window45ListenObjekt}"> <DataGrid ItemsSource="{Binding TitelListe}" AutoGenerateColumns="True" IsReadOnly="True"> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.Panel> <ItemsPanelTemplate> <DataGridRowsPresenter/> </ItemsPanelTemplate> </GroupStyle.Panel> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> </Window>
Imports System.Collections.ObjectModel Imports System.ComponentModel Public Class F_Datagrid Private rnd As New Random Public Sub New() Dim col As New ObservableCollection(Of Window46CD) For i = 1 To 10 Dim cd = New Window46CD With {.Nummer = i, .Name = $"Name {i}"} col.Add(cd) Dim cds = rnd.Next(1, 3) ' 1 oder mehrere CD's For k = 1 To cds Dim titelliste As New List(Of Window46CDNummer) For l = 1 To rnd.Next(3, 9) titelliste.Add(New Window46Titel _ With {.CDNummer = k, .TitelNummer = l, .Saenger = $"Sänger {rnd.Next(1, 100)}", .Liedname = $"Lied {l}"}) Next If cds = 1 Then cd.CDTitel = titelliste Else If cd.CDTitel Is Nothing Then cd.CDTitel = New List(Of Window46CDNummer) cd.CDTitel.Add(New Window46CDNummer With {.CDNummer = k, .titelliste = titelliste}) End If Next Next cvs.Source = col End Sub Private cvs As New CollectionViewSource Public ReadOnly Property View As ICollectionView Get Return cvs.View ' Sicht auf Liste mit CD's Angaben End Get End Property End Class Public Class Window46CD Public Property Nummer As Integer Public Property Name As String Public Property CDTitel As List(Of Window46CDNummer) End Class Public Class Window46CDNummer Public Property CDNummer As Integer Public Property TitelListe As List(Of Window46CDNummer) End Class Public Class Window46Titel Inherits Window46CDNummer Public Property TitelNummer As Integer Public Property Saenger As String Public Property Liedname As String End Class Public Class Window45CD Public Property CD As String Public Property TitelObjekt As New List(Of Window45ListenObjekt) End Class Public Class Window45ListenObjekt Public Property Projekt As String Public Property Titels As New ObservableCollection(Of Window45Titel) Public ReadOnly Property TitelListe As ICollectionView Get Dim cvs As New CollectionViewSource cvs.Source = Titels cvs.View.GroupDescriptions.Add(New PropertyGroupDescription("Projekt")) Return cvs.View End Get End Property End Class Public Class Window45Titel Public Property Projekt As String Public Property Name As String End Class
-
Hi Matthias,
Dein Code ist ein Mix aus unterschiedlichen von mir geposteten Varianten. Ich habe Deinen letzten Code völlig unverändert in ein Projekt mit Namen "Test_Programm" kopiert und das Fenster bleibt nicht weiß. Im TreeView werden die Objekte der View-Eigenschaft mit der ToString-Methode angezeigt (Test_Programm.Window46CD). Da es in Deinem Code für diesen Type kein HierarchicalDataTemplate gibt, wird nur die ToString-Methode angewandt.Nimm mal bitte eines der von mir geposteten Demos. Aber bitte, erst einmal unverändert. Sie sind alle bei mir gelaufen. Wenn eines der Beispiel dann auch bei Dir läuft, kannst Du ja schrittweise etwas verändern und immer prüfen, ob die Veränderung wirksam war.
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks