Xceed DataGrid for WPF v7.2 Documentation
Creating a Custom Spanning

The following example demonstrates how to span cells across a range of values.

XAML
Copy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">

  <Grid.Resources>

    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"

                                       Source="{Binding Source={x:Static Application.Current}, Path=Orders}" />

    <local:CustomSpannedCellSelector x:Key="spannedCellSelector" />

  </Grid.Resources>

  <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_orders}}"

                        SpannedCellSelector="{StaticResource spannedCellSelector}">

    <xcdg:DataGridControl.View>

      <xcdg:TableView AllowCellSpanning="True" />

    </xcdg:DataGridControl.View>

    <xcdg:DataGridControl.Columns>

      <xcdg:Column FieldName="Freight"

                   CellSpanningDirection="Row" />

    </xcdg:DataGridControl.Columns>

  </xcdg:DataGridControl>

</Grid> 

 

C#
Copy Code
public sealed class CustomSpannedCellSelector : SpannedCellSelector

{

  private const decimal Range = 10m;

  public override bool CanMerge(SpannedCellFragment x, SpannedCellFragment y)

  {

    if ((x.Column == y.Column) && (x.Column.FieldName == "Freight"))

    {

      var xBound = this.GetLowerBound(x);

      var yBound = this.GetLowerBound(y);

      return xBound.HasValue

             && yBound.HasValue

             && (xBound.Value == yBound.Value);

    }

    return false;

  }

  public override object SelectContent(IEnumerable<SpannedCellFragment> fragments)

  {

    var first = fragments.FirstOrDefault();

    var bound = (first != null) ? this.GetLowerBound(first) : null;

    if (!bound.HasValue)

      return null;

    return string.Format("{0} - {1}", bound.Value, bound.Value + Range);

  }

  private decimal? GetLowerBound(SpannedCellFragment x)

  {

    var content = x.Content;

    if (!(content is decimal))

      return null;

    var value = (decimal)content;

    return Math.Floor(value / Range) * Range;

  }

}