2016-10-17 7 views
0

Я пытаюсь реализовать DataGrid заголовков так же, как это изображение: enter image description hereКак настроить заголовки DataGrid, похожие на это изображение?

Как это мог быть достигнута раскладка?

+0

Какие столбцы вашей записи? Думаю, я могу вам помочь. – AnjumSKhan

+0

Не могли бы вы уточнить, используете ли вы какой-либо сторонний элемент управления DataGrid или Microsoft DataGrid? –

+0

Dublicate http://stackoverflow.com/questions/17652039/multilevel-column-header-for-datagrid-in-wpf также проверяет http://stackoverflow.com/questions/4827481/multi-level-header-gridview- wpf – Mikolaytis

ответ

0

Я всегда предпочитаю этот подход, который использует DataGrids в пределах DataGrid.

XAML

<Window x:Class="WpfStackOverflow.Window8" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window8" Height="300" Width="300"> 
    <Grid> 
     <DataGrid Loaded="Dgrd_Loaded_1" CanUserAddRows="False" CanUserResizeRows="False" VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Transparent" x:Name="Dgrd" ItemsSource="{Binding .}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <DataGrid BorderThickness="1" HorizontalScrollBarVisibility="Auto" VerticalGridLinesBrush="Transparent" HeadersVisibility="Column" AutoGenerateColumns="False" ItemsSource="{Binding SupplierIDCol}">        
           <DataGrid.Columns> 
            <DataGridTextColumn Header="Supplier ID" Binding="{Binding SupplierID}" MinWidth="100" Width="*"/> 
           </DataGrid.Columns> 
          </DataGrid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn MinWidth="300" Width="*"> 
        <DataGridTemplateColumn.Header> 
         <Grid> 
          <TextBlock Text="Product Details" HorizontalAlignment="Center" /> 
         </Grid> 
        </DataGridTemplateColumn.Header> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <DataGrid BorderThickness="1" HorizontalScrollBarVisibility="Auto" AutoGenerateColumns="False" MinWidth="300" ItemsSource="{Binding SupplierIDCol}" HorizontalContentAlignment="Stretch" HeadersVisibility="Column"> 
           <DataGrid.CellStyle> 
            <Style TargetType="DataGridCell"> 
             <Style.Triggers> 
              <DataTrigger Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="3"> 
               <Setter Property="BorderThickness" Value="0"/> 
              </DataTrigger> 
             </Style.Triggers> 
            </Style> 
           </DataGrid.CellStyle> 
           <DataGrid.Columns> 
            <DataGridTextColumn Header="ProductID" Binding="{Binding ProductID}"/> 
            <DataGridTextColumn Header="ProductName" Binding="{Binding ProductName}"/> 
            <DataGridTextColumn Header="QuantityPerUnit" Binding="{Binding QuantityPerUnit}"/> 
            <DataGridTextColumn Header="Price" Binding="{Binding Price}" MinWidth="75" Width="*"/> 
           </DataGrid.Columns> 
          </DataGrid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn MinWidth="100" Width="*"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <DataGrid BorderThickness="1" VerticalGridLinesBrush="Transparent" HeadersVisibility="Column" AutoGenerateColumns="False" ItemsSource="{Binding SupplierIDCol}"> 
           <DataGrid.Columns> 
            <DataGridTextColumn Header="Units In Stock" Binding="{Binding UnitsInStocks}" MinWidth="100" Width="*"/> 
           </DataGrid.Columns> 
          </DataGrid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

КОД

using System.Linq; 
using System.Windows; 
using System.Windows.Controls; 

namespace WpfStackOverflow 
{ 
    /// <summary> 
    /// Interaction logic for Window8.xaml 
    /// </summary> 
    public partial class Window8 : Window 
    { 
     public Window8() 
     { 
      InitializeComponent(); 

      var records = new[] { 
       new { SupplierID = 123, ProductID = 12, ProductName = "ProductName1", QuantityPerUnit = 45, Price = 451, UnitsInStocks = 123 }, 
       new { SupplierID = 123, ProductID = 12, ProductName = "ProductName1", QuantityPerUnit = 45, Price = 451, UnitsInStocks = 123 }, 
       new { SupplierID = 123, ProductID = 12, ProductName = "ProductName1", QuantityPerUnit = 45, Price = 451, UnitsInStocks = 123 }, 
       new { SupplierID = 123, ProductID = 12, ProductName = "ProductName1", QuantityPerUnit = 45, Price = 451, UnitsInStocks = 123 }, 
       new { SupplierID = 123, ProductID = 12, ProductName = "ProductName1", QuantityPerUnit = 45, Price = 451, UnitsInStocks = 123 } 
      }.ToList(); 

      this.DataContext = new[] { new { SupplierIDCol = records } }.ToList(); 
     } 

     private void Dgrd_Loaded_1(object sender, RoutedEventArgs e) 
     { 
      DataGrid dgrid = sender as DataGrid; 
      DataGridColumn col = dgrid.Columns[1]; 
      Grid header = col.Header as Grid; 

      DataGridRow row = dgrid.ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow; 

      ContentPresenter cp = col.GetCellContent(row) as ContentPresenter; 
      header.Width = cp.ActualWidth; 
     } 
    } 
}