Data Grid Extensions for WPF

repository·master·Indexed 21 days ago

https://github.com/dotnet/datagridextensions

A collection of modular, attached-property-based extensions for the WPF System.Windows.Controls.DataGrid. It enhances the standard DataGrid with advanced filtering, sorting, and Excel-like copy/paste functionality without requiring custom derived classes. Key features include auto-filtering, multi-line editing, initial sorting application, and specialized behaviors such as BeginEditOnCtrlEnterBehavior, ExtendedStarSizeBehavior, and DisableTargetWhileEditingBehavior.

Tokens
2.7K
Snippets
14
Records
16
Agent score
74%

What's inside Data Grid Extensions

  1. Overview of Data Grid Extensions features

    master

    Data Grid Extensions provides several modular features for the WPF DataGrid (System.Windows.Controls.DataGrid):

    • Filtering: Add text or boolean filtering capabilities.
    • Sorting: Apply initial sorting to columns.
    • Editing Mode Control: Disable other controls while a cell is in editing mode.
    • Column Events: Provides additional events for DataGrid columns.
    • Keyboard Shortcuts: Start editing a cell using Ctrl+Enter.
    • Column Sizing: Provides extended star-size column behavior.
    • Excel-like Copy/Paste: Methods to easily implement copy and paste functionality.
  2. Overview of Data Grid Extensions

    master

    The Data Grid Extensions component is a collection of features designed to extend the standard behavior of the DataGrid, enhancing usability through additional filtering, editing behaviors, and utility tools.

    Key feature categories include:

    • Filtering: Advanced filtering capabilities.
    • Behaviors: Custom interaction patterns like Begin Edit On Ctrl+Enter, Disable Target While Editing, and Extended Star Size Behavior.
    • Tools: Utility enhancements such as Additional Events, Apply Initial Sorting, Force Commit On Lost Focus, and Multi Line Editing.
  3. Use Data Grid Extension Behaviors

    master

    You can add specialized behaviors to a DataGrid by attaching them to the i:Interaction.Behaviors collection. This allows you to extend the default functionality of the grid, such as modifying keyboard shortcuts, column resizing logic, or controlling external UI elements during edit mode.

    To use these behaviors, ensure you have the appropriate namespaces for i:Interaction (typically from Microsoft.Xaml.Behaviors.Wpf) and dgx: (the Data Grid Extensions namespace) defined in your XAML.

    <DataGrid ItemsSource="{Binding Items}">
      <i:Interaction.Behaviors>
        <dgx:BeginEditOnCtrlEnterBehavior/>
        <dgx:ExtendedStarSizeBehavior/>
        <dgx:DisableTargetWhileEditingBehavior Target="{Binding ElementName=ToolBar}"/>
      </i:Interaction.Behaviors>
    </DataGrid>
  4. Install Data Grid Extensions via NuGet

    master

    Data Grid Extensions provides modular extensions for the WPF System.Windows.Controls.DataGrid. Instead of requiring a new derived class, it attaches features transparently to your existing DataGrid. You can install the binaries via NuGet.

    NuGet package: DataGridExtensions
  5. Customize a column's filter template

    master

    You can customize the appearance and behavior of a specific column's filter by providing a custom template via the dgx:DataGridFilterColumn.Template attached property on the column.

    <DataGridTextColumn Header="Double/Custom" 
                        Binding="{Binding Probability, Mode=OneWay}" 
                        dgx:DataGridFilterColumn.Template="{StaticResource FilterWithPopup}"/>
  6. Filter and sort Template Columns

    master

    When using a DataGridTemplateColumn, you must specify the SortMemberPath to indicate which property of the underlying data item should be used for filtering and sorting operations. You can also apply a custom filter template to the template column using dgx:DataGridFilterColumn.Template.

    <DataGridTemplateColumn Header="Template" 
                            SortMemberPath="Column5" 
                            dgx:DataGridFilterColumn.Template="{StaticResource MultipleChoiceFilter}"> 
        <DataGridTemplateColumn.CellTemplate> 
            <DataTemplate DataType="{x:Type basicSample:DataItem}" > 
                <TextBox Text="{Binding Column5}"/> 
            </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn>
  7. Enable Auto-Filtering in the WPF DataGrid

    master

    You can add filtering capabilities to your DataGrid by setting the dgx:DataGridFilter.IsAutoFilterEnabled attached property to True. The extension automatically provides text filters for string columns or boolean filters for boolean columns.

    <DataGrid ItemsSource="{Binding Items}" 
              dgx:DataGridFilter.IsAutoFilterEnabled="True"/>
  8. Customize DataGrid Filter Column Templates

    master

    To customize the appearance or behavior of a specific column's filter, you can override its template using the dgx:DataGridFilterColumn.Template attached property on the column definition.

    <DataGridTextColumn Header="Double/Custom" 
                        Binding="{Binding Probability, Mode=OneWay}" 
                        dgx:DataGridFilterColumn.Template="{StaticResource FilterWithPopup}"/>
  9. Enable automatic filtering in DataGrid

    master

    To enable filtering in a DataGrid, attach the dgx:DataGridFilter.IsAutoFilterEnabled property and set it to True. The library automatically provides text filters for string columns and boolean filters for boolean columns based on the column type.

    <DataGrid ItemsSource="{Binding Items}" 
              dgx:DataGridFilter.IsAutoFilterEnabled="True"/>
  10. Force commit cell changes on lost focus

    master

    When a DataGrid loses focus (for example, when switching tabs in a TabControl) while a cell is in edit mode, uncommitted changes may be lost. To prevent this, set dgx:Tools.ForceCommitOnLostFocus="True" on the specific DataGridTextColumn to trigger an automatic commit of the cell or row when focus is lost.

    <DataGridTextColumn dgx:Tools.ForceCommitOnLostFocus="True" />
  11. Apply initial sorting to a DataGrid

    master

    By default, if you define a SortDirection on a DataGridTextColumn, the column header will show the sorting icon, but the underlying data will not be sorted. To ensure the data is actually sorted according to the initial column definitions, set the dgx:Tools.ApplyInitialSorting attached property to True on the DataGrid element.

    <DataGrid dgx:Tools.ApplyInitialSorting="True">
      <DataGrid.Columns>
        <DataGridTextColumn Header="Initially sorted"
                            SortDirection="Descending" />
      </DataGrid.Columns>
    </DataGrid>