Back to Devexpress

CRXPF0010 - Use DependencyObjectExtensions.DataContext to define bindings for generated columns

coderushforroslyn-404294-static-code-analysis-xaml-analyzers-crxpf-0010-use-dependency-object-extensions-data-context-to-define-bindings-for-generated-columns.md

latest2.1 KB
Original Source

CRXPF0010 - Use DependencyObjectExtensions.DataContext to define bindings for generated columns

  • Feb 14, 2023

Severity: Warning

The analyzer detects that you generate columns from a collection and bind column properties (in the ColumnGeneratorTemplate or ColumnGeneratorTemplateSelector) without the DependencyObjectExtensions.DataContext attached property. In this case, the GridControl may work slower.

Examples

Invalid Code

xaml
<Window ...
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
    <dxg:GridControl ...
                     ColumnsSource="{Binding Columns}">
        <dxg:GridControl.ColumnGeneratorTemplate>
            <DataTemplate>
                <dxg:GridColumn FieldName="{Binding FieldName}"/>
            </DataTemplate>
        </dxg:GridControl.ColumnGeneratorTemplate>
    </dxg:GridControl>

Valid Code

xaml
<Window ...
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
        xmlns:dxci="http://schemas.devexpress.com/winfx/2008/xaml/core/internal">
    <dxg:GridControl ...
                     ColumnsSource="{Binding Columns}">
        <dxg:GridControl.ColumnGeneratorTemplate>
            <DataTemplate>
                <dxg:GridColumn FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"/>
            </DataTemplate>
        </dxg:GridControl.ColumnGeneratorTemplate>
    </dxg:GridControl>

How to Fix

Use the DependencyObjectExtensions.DataContext attached property to define bindings in templates used to generate columns.

Refer to the following help topic for more information: Bind the Grid to a Collection of Columns.