1

I have got my model to display like below:

enter image description here

What I am trying to achieve is that instead of having the row details show up the way they do by default, I want them to be displayed aligned to the parent grid columns.I want the details grid to skip the first 3 columns (be blank),and then have details align with the 'street name', 'street number', column of the parent row.

If someone could point me to the properties I should look into to achieve this, that would be great.

Update: I made some progress, but still not all the way through. I've got the rows displaying how I wanted, but it breaks when the column is resized. I didn't find any event related to Column width changed or similar to write a code behind to adjust the width. How is it done in WPF? Also, is there a better way to achieve this? enter image description here

The xaml code is:

<Window x:Class="AligningDetailsView.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:AligningDetailsView"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <CollectionViewSource x:Key="customerViewSource"
                          Source="{Binding Customers}"
                          d:DesignSource="{d:DesignInstance {x:Type local:Customer}, CreateList=True}" />
</Window.Resources>
<Grid>

    <DataGrid x:Name="customerDataGrid"
              ItemsSource="{Binding Source={StaticResource customerViewSource}}"
              EnableRowVirtualization="True"
              AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
              Margin="0,27,0.4,-0.2">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="firstNameColumn"
                                Width="100"
                                Header="First Name"
                                Binding="{Binding FirstName}" />
            <DataGridTextColumn x:Name="lastNameColumn"
                                Width="100"
                                Header="Last Name"
                                Binding="{Binding LastName}" />
            <DataGridTextColumn x:Name="phoneColumn"
                                Width="100"
                                Header="Age"
                                Binding="{Binding Age}" />
            <DataGridTextColumn x:Name="colStreetNumber"
                                Width="100"
                                Header="StreetNumber" />
            <DataGridTextColumn x:Name="colStreetName"
                                Width="100"
                                Header="StreetNumber" />
            <DataGridTextColumn x:Name="colState"
                                Width="100"
                                Header="State" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Addresses}"
                          RowHeaderWidth="300"
                          HeadersVisibility="Row"
                          AutoGenerateColumns="False">

                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="detail1"
                                 Binding="{Binding StreetNumber}"            Width="100" />
                        <DataGridTextColumn x:Name="detail2"
                                            Width="100" Binding="{Binding StreetName}" />
                        <DataGridTextColumn x:Name="detail3"
                                            Width="100" Binding="{Binding State}" />
                    </DataGrid.Columns>
                </DataGrid>

            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
</Grid>

and the code behind is:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        Customers = GetCustomers();
        InitializeComponent();
    }

    private List<Customer> GetCustomers()
    {
        return new List<Customer>()
        {
            new Customer()
            {
                FirstName = "James",
                LastName = "aka",
                Age = 22,
                Addresses = new List<Address>()
                {
                    new Address()
                    {
                        StreetName = "abdf st",
                        StreetNumber = 123,
                        State = "OH"
                    },
                    new Address()
                    {
                        StreetName = "skdjf abdf st",
                        StreetNumber = 12233,
                        State = "OH"
                    }
                }
            }
        };
    }
    public List<Customer> Customers { get; set; }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public int StreetNumber { get; set; }
    public string StreetName { get; set; }
    public string State { get; set; }
}
haku
  • 4,105
  • 7
  • 38
  • 63

1 Answers1

0

You can remove the row header, which is the small gap on the left of the child row:

<DataGrid RowHeaderWidth="50" ... >
benPearce
  • 37,735
  • 14
  • 62
  • 96
  • What I meant was I want the details row to be underneath the street name, street number etc column headings that are on the first line instead of having them displayed as a grid of their own. – haku Apr 11 '16 at 23:05
  • Start by modifying your question to be clearer and add some Xaml and code to show what you have already done. – benPearce Apr 12 '16 at 00:36
  • benPearce, I made some progress based on that RowHeaderWidth info you gave. I've also updated my question with the code. I was thinking of adding code to an event like ColumnHeaderWidthChanged, but there doesn't seem to be such available. Did you happen to know how I could go about getting the details column width adjusted when user changes the parent column width? Thanks. – haku Apr 12 '16 at 01:43