DataGrid Beta 2 Breaking Changes

This topic discusses the changes made to the Silverlight DataGrid control between the Microsoft Silverlight 2 Beta 1 and the Beta 2 releases. The changes discussed in this article are focused on changes that might cause your older Silverlight-based applications to now fail or behave differently, not on new features/enhancements for this release.

For more information, see Breaking Changes Between Beta 1 and Beta 2.

AutoGenerateColumns behavior modified

Who Is Affected: Silverlight 2 Beta 1 managed applications that use the DataGrid.

Summary

The behavior for auto column generation was drastically modified.  See the differences below.

Fix Required

Users using the DataGrid may or may not need to modify their code to turn dataGrid.AutoGenerateColumns on or off.  Additionally, users depending on columns to be autogenerated instantaneously may need to change their code to wait for the Loaded event or use the AutogeneratingColumn event.

 

Beta 1

  • dataGrid.AutoGenerateColumns is false by default
  • The DataGrid generates columns instantaneously once per ItemsSource
  • Setting AutoGenerateColumns from true to false does not affect the existing columns
  • Setting AutoGenerateColumns from false to true will generate columns if columns have not been previously generated for the ItemsSource
  • When the DataGrid autogenerates columns, all existing columns are first cleared before generating new ones regardless of whether they were autogenerated or not
  • Autogenerated columns are no different than user columns

 

Beta 2

  • dataGrid.AutoGenerateColumns is true by default
  • Columns have a public readonly flag IsAutoGenerated that returns true for Autogenerated columns
  • Column generation is delayed until the DataGrid has loaded to avoid generating columns for the following sequence before the DataGrid is loaded:

dataGrid.ItemsSource = myList;

dataGrid.AutoGenerateColumns = false;

  • If AutoGenerateColumns is set from true to false at runtime, the generated columns are removed from the columns collection
  • The DataGrid always regenerates columns when AutoGenerateColumns is set from false to true at runtime
  • When the DataGrid autogenerates columns, all existing autogenerated columns are first removed from the columns collection. The newly generated columns are appened to the end of the Columns after the user defined columns

 

DependencyProperty DataGrid.SelectedItemsProperty removed

Who Is Affected: Silverlight 2 managed applications that use the DataGrid.SelectedItemsProperty dependency property.

Summary

The SelectedItems can be modified the collection’s methods, but the property itself is a readonly property.  In Beta1, the property was backed by a DependencyProperty that threw an InvalidOperationException when it was set.  The motivation for doing so came from the WPF ListBox, but in talking to WPF we decided that what the ListBox does is wrong.  Instead of a DependencyProperty, it is now a readonly CLR property

Fix Required

SelectedItems can no longer be used as a DependencyProperty

 

PreparingRow/CleaningRow renamed to LoadingRow/UnloadingRow

Who Is Affected: Silverlight 2 Beta 1 managed applications listening to dataGrid.PreparingRow or dataGrid.CleaningRow.

Summary

The PreparingRow and the CleaningRow events have been renamed to LoadingRow and UnloadingRow.  In addition, CleaningRow event is no longer cancelable.

Fix Required

Use LoadingRow and UnloadingRow instead of PreparingRow and CleaningRow.

 

Beta 1

[C#]

dataGrid.PreparingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_PreparingRow);

dataGrid.CleaningRow += new EventHandler<DataGridRowCancelEventArgs>(dataGrid_CleaningRow);

 

Beta 2

[C#]

dataGrid.LoadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_LoadingRow);

dataGrid.UnloadingRow += new EventHandler<DataGridRowEventArgs>(dataGrid_UnloadingRow);

 

DataGridColumnBase renamed to DataGridColumn

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGridColumnBase type explicitly.

Summary

DataGridColumnBase was renamed to DataGridColumn.

Fix Required

Any explicit uses of DataGridColumnBase require a name change to DataGridColumn.

 

Beta 1

[C#]

DataGridColumnBase column = dataGrid.Columns[0];

 

Beta 2

[C#]

DataGridColumn column = dataGrid.Columns[0];

 

DataGridBoundColumnBase renamed to DataGridBoundColumn

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGridBoundColumnBase type explicitly.

Summary

DataGridBoundColumnBase was renamed to DataGridBoundColumn.

Fix Required

Any explicit uses of DataGridBoundColumnBase require a name change to DataGridBoundColumn.

 

Beta 1

[C#]

DataGridBoundColumnBase boundColumn = dataGrid.Columns[0] as DataGridBoundColumnBase;

 

Beta 2

[C#]

DataGridBoundColumn boundColumn = dataGrid.Columns[0] as DataGridBoundColumn;

 

DataGridTextBoxColumn renamed to DataGridTextColumn

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGridTextBoxColumn type explicitly.

Summary

DataGridTextBoxColumn was renamed to DataGridTextColumn.

Fix Required

Any explicit uses of DataGridTextBoxColumn require a name change to DataGridTextColumn.

 

Beta 1

[C#]

((DataGridTextBoxColumn)dataGrid.Columns[0]).FontSize = 11;

 

Beta 2

[C#]

((DataGridTextColumn)dataGrid.Columns[0]).FontSize = 11;

 

HeaderTemplate property removed

Who Is Affected: Silverlight 2 Beta 1 managed applications using the dataGrid.HeaderTemplate property.

Summary

The HeaderTemplate property which could be applied to RowHeaders or ColumnHeaders was removed.

Fix Required

Users previously using HeaderTemplate for RowHeaders or ColumnHeaders will use dataGrid.RowHeaderStyle to set the template for RowHeaders or dataGrid.ColumnHeaderStyle to set the template for ColumnHeaders.  Alternatively, users could set the style or template on individual RowHeaders or ColumnHeaders explicitly.

 

RowDetailsVisibility renamed to RowDetailsVisibilityMode

Who Is Affected: Silverlight 2 Beta 1 managed applications using the dataGrid.RowDetailsVisibility enumeration.

Summary

RowDetailsVisibility was renamed to RowDetailsVisibilityMode to better reflect what it is and not imply that it is of type Visibility.

Fix Required

Any use of RowDetailsVisibility requires a name change to RowDetailsVisibilityMode.

 

Beta 1

[C#]

dataGrid.RowDetailsVisibility = RowDetailsVisibility.VisibleWhenSelected;

 

Beta 2

[C#]

dataGrid.RowDetailsVisibilityMode = RowDetailsVisibilityMode.VisibleWhenSelected;

 

CheckBoxContentBinding changed to Content

Who Is Affected: Silverlight 2 Beta 1 managed applications using dataGridCheckBoxColumn.CheckBoxContentBinding.

Summary

There is no need for a Binding in this case so we’ve changed this to an object named Content that maps to the Content property of the CheckBoxes within the column

Fix Required

Users using CheckBoxContentBinding need to change the Binding to the content itself.

 

Beta 1

[C#]

dataGridCheckBoxColumn.CheckBoxContentBinding = new Binding(“checkBoxContent”);

 

Beta 2

[C#]

dataGridCheckBoxColumn.Content = checkBoxContent;

 

GetElement renamed to GetCellContent

Who Is Affected: Silverlight 2 Beta 1 managed applications using DataGridColumnBase.GetCellContent.

Summary

The GetElement method on DataGridColumn that retrieves content from the cell of a given DataGridRow or DataItem renamed to GetCellContent.

Fix Required

User calling GetElement need to change their calls to GetCellContent.

 

Beta 1

[C#]

dataGrid.Columns[0].GetElement(myItems[0]);

 

Beta 2

[C#]

dataGrid.Columns[0].GetCellContent(myItems[0]);

 

UpdateElement renamed to RefreshCellContent

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGrid with custom or template columns

Summary

The UpdateElement method on DataGridColumnBase was renamed to RefreshCellContent.

Fix Required

Users override UpdateElement in their columns need to override RefreshCellContent instead

Beta 1

[C#]

public MyColumn : DataGridBoundColumnBase

{

      public override void UpdateElement(Frameworkelement element, string propertyName) {…}

}

 

Beta 2

[C#]

public MyColumn : DataGridBoundColumn

{

      public override void RefreshCellContent(Frameworkelement element, string propertyName) {…}

}

 

UpdateElements renamed to NotifyPropertyChanged

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGrid with their own custom columns

Summary

The UpdateElements method on DataGridBoundColumnBase was renamed to NotifyPropertyChanged.

Fix Required

Users using the UpdateElements method within a custom column need to now use NotifyPropertyChanged.

Beta 1

[C#]

public MyColumn : DataGridBoundColumnBase

{

  public double FontSize

        {

            get {…}

            set

            {

                    this._fontSize = value;

                    UpdateElements(“FontSize”);

            }

        }

}

 

Beta 2

[C#]

public MyColumn : DataGridBoundColumn

{

  public double FontSize

        {

            get {…}

            set

            {

                    this._fontSize = value;

                    NotifyPropertyChanged(“FontSize”);

            }

        }

}

 

dataItem parameter added to GenerateElement and GenerateEditingElement

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGrid with custom bound columns

Summary

The GenerateElement and GenerateEditingElement methods on DataGridBoundColumnBase now take in the dataItem that the row containg the element is bound to.

Fix Required

Users overriding GenerateElement and/or GenerateEditingElement in their custom bound columns need to add the dataItem parameter

Beta 1

[C#]

public MyColumn : DataGridBoundColumnBase

{

      public override void GenerateElement() {…}

      public override void GenerateEditingElement() {…}

}

 

Beta 2

[C#]

public MyColumn : DataGridBoundColumn

{

      public override void GenerateElement(object dataItem) {…}

      public override void GenerateEditingElement(object dataItem) {…}

}

 

Index on DataGridBoundColumnBase made internal

Who Is Affected: Silverlight 2 Beta 1 managed applications using the dataGridColumnBase.Index property.

Summary

There is no need to have both Index and DisplayIndex exposed for DataGridColumns.  Index is an implementation detail so it was made internal.

Fix Required

Users can no longer use the Index property for DataGridColumns.  They should use DisplayIndex to reorder columns and access the DisplayMemberBinding if they want to know which field in the DataItem the column is bound to.

 

dataGrid.GetRowDetailsVisibility changed to DataGridRow.GetRowContainingElement

Who Is Affected: Silverlight 2 Beta 1 managed applications using the GetRowDetailsVisibility method on the DataGrid.

Summary

Instead of a method on the DataGrid that returns the visibility of the row details of a row given an element inside the row, we created a more flexible method that returns the row given an element inside the row.  Users can then check the row’s DetailsVisibility property for this particular scenario.

Fix Required

Users using dataGrid.GetRowDetailsVisibility need to update the call as follows.

 

Beta 1

[C#]

Visibility detailsVisibility = dataGrid.GetRowDetailsVisibility(clickedButton);

 

Beta 2

[C#]

DataGridRow clickedRow = DataGridRow.GetRowContainingElement(clickedButton);

Visibility detailsVisibility = clickedRow.DetailsVisibility;

 

dataGrid.GetRowDetailsVisibility changed to DataGridRow.GetRowContainingElement

Who Is Affected: Silverlight 2 Beta 1 managed applications using the GetRowDetailsVisibility method on the DataGrid.

Summary

Instead of a method on the DataGrid that returns the visibility of the row details of a row given an element inside the row, we created a more flexible method that returns the row given an element inside the row.  Users can then check the row’s DetailsVisibility property for this particular scenario.

Fix Required

Users using dataGrid.GetRowDetailsVisibility need to update the call as follows.

 

Beta 1

[C#]

Visibility detailsVisibility = dataGrid.GetRowDetailsVisibility(clickedButton);

 

Beta 2

[C#]

DataGridRow clickedRow = DataGridRow.GetRowContainingElement(clickedButton);

Visibility detailsVisibility = clickedRow.DetailsVisibility;

 

Column Width changed from double to DataGridLength

Who Is Affected: Silverlight 2 Beta 1 managed applications setting dataGridColumnBase.Width outside of xaml.

Summary

To support various sizing options for column widths, dataGridColumnBase.Width changed from being being a double to a DataGridLength.

Fix Required

Users using setting dataGridColumnBase.Width outside of xaml will need to instantiate a DataGridLength or use one of the static DataGridLengths (DataGridLength.Auto, DataGridLength.SizeToHeader, DataGridLength.SizeToCells) instead of setting it to a double.

 

Beta 1

[C#]

dataGrid.Columns[0].Width = 20;

[Xaml]

<DataGridTextBoxColumn Width=”20” DisplayMemeberBinding=”{Binding foo}” />

 

Beta 2

[C#]

dataGrid.Columns[0].Width = new DataGridLength(20);

dataGrid.Columns[0].Width = DataGridLength.Auto;

dataGrid.Columns[0].Width = DataGridLength.SizeToHeader;

dataGrid.Columns[0].Width = DataGridLength.SizeToCells;

[Xaml]

<DataGridTextColumn Width=”20” DisplayMemeberBinding=”{Binding foo}” />

<DataGridTextColumn Width=”Auto” DisplayMemeberBinding=”{Binding bar}” />

<DataGridTextColumn Width=”SizeToHeader” DisplayMemeberBinding=”{Binding hello}” />

<DataGridTextColumn Width=”SizeToCells” DisplayMemeberBinding=”{Binding hello}” />

 

OverrideRowDetailsScrolling renamed to AreRowDetailsFrozen

Who Is Affected: Silverlight 2 Beta 1 managed applications using dataGrid.OverrideRowDetailsScrolling.

Summary

The OverrideRowDetailsScrolling property on the DataGrid was renamed to AreRowDetailsFrozen.

Fix Required

Users using OverrideRowDetailsScrolling will need to change the name to AreRowDetailsFrozen.

 

Beta 1

[C#]

dataGrid.OverrideRowDetailsScrolling = true;

 

Beta 2

[C#]

dataGrid.AreRowDetailsFrozen = true;

 

ColumnHeadersHeight renamed to ColumnHeaderHeight

Who Is Affected: Silverlight 2 Beta 1 managed applications using dataGrid.ColumnHeadersHeight.

Summary

The ColumnHeadersHeight property on the DataGrid was renamed to ColumnHeaderHeight.

Fix Required

Users using ColumnHeadersHeight will need to change the name to ColumnHeaderHeight.

 

Beta 1

[C#]

dataGrid.ColumnHeadersHeight = 25;

 

Beta 2

[C#]

dataGrid.ColumnHeaderHeight = 25;

 

RowHeadersWidth renamed to RowHeaderWidth

Who Is Affected: Silverlight 2 Beta 1 managed applications using dataGrid.RowHeadersWidth.

Summary

The RowHeadersWidth property on the DataGrid was renamed to RowHeaderWidth.

Fix Required

Users using RowHeadersWidth will need to change the name to RowHeaderWidth.

 

Beta 1

[C#]

dataGrid.RowHeadersWidth = 30;

 

Beta 2

[C#]

dataGrid.RowHeaderWidth = 30;

 

Editing API changes

Who Is Affected: Silverlight 2 Beta 1 managed applications using the DataGrid and hooking in to the Editing interface

Summary

The editing interface went through drastic changes see the summary below.

Fix Required

Users using the DataGrid and hooking into the editing interface need to update their code base on the new editing API.

 

Beta 1

The following DataGrid Beta1 editing API has been replaced with the Beta2 API below

public class DataGrid : Control

{

     // Events

     public event EventHandler<DataGridCellEditingCancelEventArgs> BeginningCellEdit;

     public event EventHandler<DataGridCellEventArgs> CommitCellEdit;

     public event EventHandler<DataGridCellCancelEventArgs> CommittingCellEdit;

     public event EventHandler<DataGridRowCancelEventArgs> CommittingRowEdit;

 

     protected virtual void OnBeginningCellEdit(DataGridCellEditingCancelEventArgs e);

     protected virtual void OnCommitCellEdit(DataGridCellEventArgs e);

     protected virtual void OnCommittingCellEdit(DataGridCellCancelEventArgs e);

     protected virtual void OnCommittingRowEdit(DataGridRowCancelEventArgs e); 

}

 

Beta 2

The new Beta2 API for editing is as follows:

public class DataGrid : Control

{

      // Events

      public event EventHandler<DataGridBeginningEditEventArgs> BeginningEdit;

      public event EventHandler<DataGridEndingEditEventArgs> CancelingEdit;

      public event EventHandler<DataGridEndingEditEventArgs> CommittingEdit;

 

      protected virtual void OnBeginningEdit(DataGridEditingEventArgs e);    

      protected virtual void OnCommittingEdit(DataGridEndingEditEventArgs  e);

      protected virtual void OnCancelingEdit(DataGridEndingEditEventArgs e);

}

 

public enum DataGridEditingUnit

{

      Cell,

      Row

}

 

Template changes for DataGrid classes

Who Is Affected: Silverlight 2 Beta 1 managed applications that re-template the DataGrid and/or related components

Summary

The default templates for the DataGrid and its related classes have changed since Beta1 to Beta2.

Fix Required

Users re-templating the DataGrid will need to re-base their templates using the new Beta 2 templates.  The new templates will be available in the Beta 2 update of the DataGrid Styles and Templates topic.