1.
<Window x:Class="WpfApp7.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:WpfApp7" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" > <Grid> <DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="First Name"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding FirstName}"> <TextBlock.ToolTip> <StackPanel> <TextBlock Text="{Binding FirstName}"></TextBlock> <TextBlock Text="{Binding LastName}"></TextBlock> <TextBlock Text="{Binding EmailId}"></TextBlock> <TextBlock Text="{Binding Contact}"></TextBlock> </StackPanel> </TextBlock.ToolTip> </TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></DataGridTextColumn> <DataGridTextColumn Header="Email Id" Binding="{Binding EmailId}"></DataGridTextColumn> <DataGridTextColumn Header="Contact" Binding="{Binding Contact}"></DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
namespace WpfApp7 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ObservableCollection<Employee> empList = new ObservableCollection<Employee>(); for (int i = 0; i < 20; i++) { Employee emp = new Employee { FirstName = "FirstName" + i, LastName = "LastName" + i, EmailId = "EmailId" + i, Contact = "Contact" + i, }; empList.Add(emp); } dataGrid.ItemsSource = empList; } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string EmailId { get; set; } public string Contact { get; set; } } }
2.
<Window x:Class="WpfApp7.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:WpfApp7" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" > <Grid> <DataGrid Name="grid" AutoGenerateColumns="False"> <DataGrid.RowDetailsTemplate> <DataTemplate> <TextBlock Background="Orange" Text="{Binding Contact}" TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> </DataTemplate> </DataGrid.RowDetailsTemplate> <DataGrid.Columns> <DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" /> <DataGridTextColumn Header="LastName" Binding="{Binding LastName}" /> <DataGridTextColumn Header="EmailId" Binding="{Binding EmailId}" /> </DataGrid.Columns> </DataGrid> </Grid> </Window>
namespace WpfApp7 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ObservableCollection<Employee> empList = new ObservableCollection<Employee>(); for (int i = 0; i < 20; i++) { Employee emp = new Employee { FirstName = "FirstName" + i, LastName = "LastName" + i, EmailId = "EmailId" + i, Contact = "Contact" + i, }; empList.Add(emp); } grid.ItemsSource = empList; } } public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string EmailId { get; set; } public string Contact { get; set; } } }