Control - DATA-GRID

 

 

Klasse: Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFDataGrid
{
public class Person
{
public string Vorname { get; set; }
public string Nachname { get; set; }
}
}

 

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFDataGrid
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }

public MainWindow()
{
InitializeComponent();

Person person1 = new Person { Vorname = "Max", Nachname = "Mustermann" };
Person person2 = new Person { Vorname = "Han", Nachname = "Solo" };
Person person3 = new Person { Vorname = "Luke", Nachname = "Skywalker" };
Person person4 = new Person { Vorname = "Darth", Nachname = "Vader" };

Persons = new List<Person> { person1, person2, person3, person4 };

//WICHTIG !!!
DataContext = this;

}
}
}

 

MainWindow.xaml
<Window x:Class="WPFDataGrid.MainWindow"
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:WPFDataGrid"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding Vorname}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Nachname}" />
</DataGrid.Columns>

<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="1" Background="LightGray" Padding="10">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Nachname:" FontSize="12" VerticalAlignment="Center" Width="80"/>
<TextBlock Text="{Binding Nachname}" FontSize="16" FontWeight="ExtraBold" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Vorname:" FontSize="12" VerticalAlignment="Center" Width="80"/>
<TextBlock Text="{Binding Vorname}" FontSize="14" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>

</DataGrid>
</Grid>
</Window>

 

Top