Search code examples
c#wpfexpression-blend

Show DataGrid row details by clicking row header?


Normally row details are show when you click on a row. I want to disable this so that clicking a DataGrid row simply selects it but doesn't show the row details. I want the row details to be shown when a user clicks the row header. How would I do this?


Solution

  • To stop someone for getting the description when the row is clicked one adds this attribute to the grid:

    <DataGrid Name="dgPrimary" RowDetailsVisibilityMode="Collapsed">
    

    Then one adds two behaviors of type ChangePropertyAction, using expression blend is one of the easiest.

    1. Mouse enter action
      Properties Window in Blend for Mouse Enter

    2. Mouse leave action
      Properties Window in Blend for Mouse Leave

      Here is the original datagrid before the mouse moves over the headers

      enter image description here



    Here is when I moved the mouse over my test project's datagrid dgPrimary. See how all the rows description opened up.
    Result datagrid when mouse moves over it

    Here is the code tacked on from blend to the datagrid

    <DataGrid x:Name="dgPrimary" RowDetailsVisibilityMode="Collapsed">
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel Margin="20,0,0,0" Orientation="Horizontal">
                    <TextBlock FontWeight="Bold" Text="{Binding New}" />
    
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Original}"
                                Header="File Name Before"
                                IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding New}"
                                Header="File Name After"
                                IsReadOnly="True" />
        </DataGrid.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseEnter">
                <ei:ChangePropertyAction PropertyName="RowDetailsVisibilityMode">
                    <ei:ChangePropertyAction.Value>
                        <DataGridRowDetailsVisibilityMode>Visible</DataGridRowDetailsVisibilityMode>
                    </ei:ChangePropertyAction.Value>
                </ei:ChangePropertyAction>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseLeave">
                <ei:ChangePropertyAction x:Name="cpaLeave" PropertyName="RowDetailsVisibilityMode"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>
    

    And the code behind to load the datagrid

    dgPrimary.ItemsSource = Directory.GetFiles( @"C:\" )
                                        .Select( ( nm, index ) => new
                                        {
                                            Original = System.IO.Path.GetFileName( nm ),
                                            New = string.Format( "{0}_{1}{2}", System.IO.Path.GetFileNameWithoutExtension( nm ), index, System.IO.Path.GetExtension( nm ) )
                                        } );