ComponentOne DataGrid for WPF and Silverlight
DataGrid for WPF and Silverlight Overview / DataGrid Features / Custom Columns / Customizing Column Cell Content
In This Topic
    Customizing Column Cell Content
    In This Topic

    In this section you'll find information about changing the UI element shown as the content of cells belonging to a column when the cell is not in editing mode.

    It’s important to note that cell content UI elements are recycled by the data grid; that means that this column could potentially use UI elements created by other columns.

    To implement custom cell content you'll need to override the following methods:

    In the implementation of a hyperlink column the methods might look similar to the example below. In the following method a different key for this column is returned (the default key is typeof(TextBlock)), That means this column will not share the cell content element with other columns (unless it would be another column which returned the same key, but that's not likely to happen).

     

    Visual Basic
    Copy Code
    Public Overloads Overrides Function GetCellContentRecyclingKey(ByVal row As DataGridRow) As Object
        Return (GetType(HyperlinkButton))
    End Function
    

     

    C#
    Copy Code
    public override object GetCellContentRecyclingKey(DataGridRow row)
    {
        return typeof(HyperlinkButton);
    }
    

     

    The CreateCellContent method will be called by the data grid if there is no recycled hyperlink. In this case a new hyperlink will be created which will be used in the cell once the cell that contains the hyperlink is unloaded; the hyperlink will be saved to be used in a future cell:
    Visual Basic
    Copy Code
    Public Overloads Overrides Function CreateCellContent(ByVal row As DataGridRow) As FrameworkElement
        Return New HyperlinkButton()
    End Function
    

     

    C#
    Copy Code
    public override FrameworkElement CreateCellContent(DataGridRow row)
    {
        return new HyperlinkButton();
    }
    

     

    After the hyperlink is created or a recycled one is taken, the BindCellContent method will be called by the data grid passing the hyperlink as a parameter. In this method you should set the properties of the hyperlink to bind it to the data of the cell:

    Visual Basic
    Copy Code
    Public Overloads Overrides Sub BindCellContent(ByVal cellContent As FrameworkElement, ByVal row As DataGridRow)
        Dim hyperlink = DirectCast(cellContent, HyperlinkButton)
        If Binding IsNot Nothing Then
            Dim newBinding As Binding = CopyBinding(Binding)
            newBinding.Source = row.DataItem
            hyperlink.SetBinding(HyperlinkButton.NavigateUriProperty, newBinding)
        End If
        hyperlink.HorizontalAlignment = HorizontalAlignment
        hyperlink.VerticalAlignment = VerticalAlignment
    End Sub
    
    C#
    Copy Code
    public override void BindCellContent(FrameworkElement cellContent, DataGridRow row)
    {
        var hyperlink = (HyperlinkButton)cellContent;
        if (Binding != null)
        {
            Binding newBinding = CopyBinding(Binding);
            newBinding.Source = row.DataItem;
            hyperlink.SetBinding(HyperlinkButton.NavigateUriProperty, newBinding);
        }
        hyperlink.HorizontalAlignment = HorizontalAlignment;
        hyperlink.VerticalAlignment = VerticalAlignment;
    }
    

    Note that you can also set the data item as the data context of the hyperlink instead of setting it in the Source property of the binding. For example:

    Visual Basic
    Copy Code
    Hyperlink.DataContext = row.DataItem
    

    C#
    Copy Code
    Hyperlink.DataContext = row.DataItem;
    

    Although you will end up with the same result, this technique does not perform as well as setting the binding source property directly.