Файл MainWindow.xaml
<Window x:Class="WpfDataGrids.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:WpfDataGrids"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<!-- data DataGrid -->
<DataGrid
AutoGenerateColumns="False"
Margin="10"
IsReadOnly="True"
ItemsSource="{Binding Customers}"
FontFamily="Gisha" FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName}" Width="270" />
<DataGridTextColumn Header="Country" Binding="{Binding CustomerCountry}" Width="150"/>
<DataGridTextColumn Header="Project Name" Binding="{Binding ProjectName}" Width="150"/>
</DataGrid.Columns>
<!-- row detail -->
<DataGrid.RowDetailsTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- photo -->
<Image Source="{Binding CustomerPhoto}" VerticalAlignment="Top"></Image>
<!-- tasks -->
<TextBlock FontSize="16" FontWeight="Bold">Tasks:</TextBlock>
<!-- sub DataGrid -->
<DataGrid
AutoGenerateColumns="False"
Margin="10"
IsReadOnly="True"
ItemsSource="{Binding Tasks}"
FontFamily="Gisha" FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Task Name" Binding="{Binding TaskName}" Width="150" />
<DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="150"/>
<DataGridTextColumn Header="Estimation (days)" Binding="{Binding Estimation}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
Файл MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace WpfDataGrids
{
public class Task
{
public string TaskName { get; set; }
public string Status { get; set; }
public int Estimation { get; set; }
}
public class Customer
{
public string CustomerName { get; set; }
public string CustomerCountry { get; set; }
public string CustomerPhoto { get; set; }
public string ProjectName { get; set; }
public Task[] Tasks { get; set; }
}
public partial class MainWindow : Window
{
public ObservableCollection<Customer> Customers { get; set; }
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
// data for datagrid
Customers = new ObservableCollection<Customer>()
{
new Customer() {
CustomerName="Daniel", CustomerCountry="Germany", CustomerPhoto="D:/Daniel.jpg", ProjectName = "AirLines",
Tasks = new Task[] {
new Task(){TaskName = "Create login page", Status = "Finished", Estimation = 3 },
new Task(){TaskName = "Update seats page", Status = "In Progrsss", Estimation = 2 },
new Task(){TaskName = "Implement algorithm", Status = "Not Started", Estimation = 4 },
new Task(){TaskName = "Writing tests", Status = "Not Started", Estimation = 3 },
} },
new Customer() {
CustomerName="Johanes", CustomerCountry="USA", CustomerPhoto="D:/Johanes.jpg", ProjectName = "Book shop",
Tasks = new Task[] {
new Task(){TaskName = "Adding db", Status = "In Progrsss", Estimation = 3 },
new Task(){TaskName = "Query records", Status = "Not Started", Estimation = 3 },
} },
new Customer() {
CustomerName="Rebeca", CustomerCountry="Poland", CustomerPhoto="D:/Rebeca.jpg", ProjectName = "Food site",
Tasks = new Task[] {
new Task(){TaskName = "Create dishes list", Status = "Finished", Estimation = 5 },
new Task(){TaskName = "Add tests", Status = "In Progrsss", Estimation = 2 },
new Task(){TaskName = "Check resolutions", Status = "Not Started", Estimation = 1 },
} }
};
}
}
}
Файл MainWindow.xaml
<Window x:Class="WpfDataGrids.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:WpfDataGrids"
xmlns:l="clr-namespace:WpfDataGrids"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<l:BoolToVisibilityConverter x:Key="boolToVisibility" />
</Window.Resources>
<Grid>
<!-- data DataGrid -->
<DataGrid
AutoGenerateColumns="False"
Margin="10"
IsReadOnly="True"
RowDetailsVisibilityMode="Visible"
ItemsSource="{Binding Customers}"
SelectedItem="{Binding SelectedCustomer}"
FontFamily="Gisha" FontSize="20">
<!-- LeftClick will Expand (by settting flag IsVisible = true) / Collapse (by settting flag IsVisible = false) -->
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftClick"
Command="{Binding ClickMouseOnCustomer}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName}" Width="270" />
<DataGridTextColumn Header="Country" Binding="{Binding CustomerCountry}" Width="150"/>
<DataGridTextColumn Header="Project Name" Binding="{Binding ProjectName}" Width="150"/>
</DataGrid.Columns>
<!-- row detail -->
<DataGrid.RowDetailsTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal"
Visibility="{Binding IsVisible, Converter={StaticResource boolToVisibility}}">
<!-- photo -->
<Image Source="{Binding CustomerPhoto}" VerticalAlignment="Top"></Image>
<!-- tasks -->
<TextBlock FontSize="16" FontWeight="Bold">Tasks:</TextBlock>
<!-- sub DataGrid -->
<DataGrid
AutoGenerateColumns="False"
Margin="10"
IsReadOnly="True"
ItemsSource="{Binding Tasks}"
FontFamily="Gisha" FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Task Name" Binding="{Binding TaskName}" Width="150" />
<DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="150"/>
<DataGridTextColumn Header="Estimation (days)" Binding="{Binding Estimation}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
Файл MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfDataGrids
{
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var bVal = (bool)value;
return bVal ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Task
{
public string TaskName { get; set; }
public string Status { get; set; }
public int Estimation { get; set; }
}
public class Customer : INotifyPropertyChanged
{
public string CustomerName { get; set; }
public string CustomerCountry { get; set; }
public string CustomerPhoto { get; set; }
public string ProjectName { get; set; }
public Task[] Tasks { get; set; }
private bool _isVisible;
public bool IsVisible { get => _isVisible; set => Set(ref _isVisible, value); }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
protected virtual bool Set<T>(ref T field, T value, [CallerMemberName] string PropertyName = null)
{
if (Equals(field, value)) return false;
field = value;
OnPropertyChanged(PropertyName);
return true;
}
}
public class MyClickCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private readonly Action<object> _callback;
public MyClickCommand(Action<object> callback)
{
_callback = callback;
}
public virtual void Execute(object parameter)
{
_callback(parameter);
}
public virtual bool CanExecute(object parameter) { return true; }
}
public partial class MainWindow : Window
{
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get
{
return _selectedCustomer;
}
set
{
_selectedCustomer = value;
if (_selectedCustomer!=null)
_selectedCustomer.IsVisible = !_selectedCustomer.IsVisible;
}
}
public ObservableCollection<Customer> Customers { get; set; }
// button handler
public ICommand ClickMouseOnCustomer { get; }
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
ClickMouseOnCustomer = new MyClickCommand((param) => {
var customer = _selectedCustomer;
if (customer != null)
{
customer.IsVisible =!customer.IsVisible;
}
});
// data for grid
Customers = new ObservableCollection<Customer>()
{
new Customer() {
CustomerName="Daniel", CustomerCountry="Germany", CustomerPhoto="D:/Daniel.jpg", ProjectName = "AirLines",
Tasks = new Task[] {
new Task(){TaskName = "Create login page", Status = "Finished", Estimation = 3 },
new Task(){TaskName = "Update seats page", Status = "In Progrsss", Estimation = 2 },
new Task(){TaskName = "Implement algorithm", Status = "Not Started", Estimation = 4 },
new Task(){TaskName = "Writing tests", Status = "Not Started", Estimation = 3 },
} },
new Customer() {
CustomerName="Johanes", CustomerCountry="USA", CustomerPhoto="D:/Johanes.jpg", ProjectName = "Book shop",
Tasks = new Task[] {
new Task(){TaskName = "Adding db", Status = "In Progrsss", Estimation = 3 },
new Task(){TaskName = "Query records", Status = "Not Started", Estimation = 3 },
} },
new Customer() {
CustomerName="Rebeca", CustomerCountry="Poland", CustomerPhoto="D:/Rebeca.jpg", ProjectName = "Food site",
Tasks = new Task[] {
new Task(){TaskName = "Create dishes list", Status = "Finished", Estimation = 5 },
new Task(){TaskName = "Add tests", Status = "In Progrsss", Estimation = 2 },
new Task(){TaskName = "Check resolutions", Status = "Not Started", Estimation = 1 },
} }
};
}
}
}