Changing Foreground of cells that are modified in DataGrid

Posted on October 5, 2008. Filed under: Silverlight |

Lets say you have a DataGrid in a page and want to show the cells with changed values as in this forum post.

Before edit

after edit

here is one way. (It wont work with DataGrid with Scrollbars)

Sample xaml

<UserControl xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”
             x:Class=”SLDataGridSample.Page”
             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:vsm=”clr-namespace:System.Windows;assembly=System.Windows”
             xmlns:localprimitives=”clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data”
             Width=”400″
             Height=”300″>
    <UserControl.Resources>
    </UserControl.Resources>
    <Grid x:Name=”LayoutRoot”
          Background=”White”>
        <data:DataGrid x:Name=”grid1″
                       AutoGenerateColumns=”False”>
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Width=”100″
                                         Header=”FirstName”
                                         Binding=”{Binding FirstName}”>

                </data:DataGridTextColumn>
                <data:DataGridTextColumn Header=”LastName”
                                         Binding=”{Binding LastName}”></data:DataGridTextColumn>
                <data:DataGridTextColumn Header=”HomeTown”
                                         Binding=”{Binding HomeTown}”></data:DataGridTextColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>

    </Grid>
</UserControl>

 public Page()
        {
            InitializeComponent();
            grid1.ItemsSource = new People();
            grid1.PreparingCellForEdit += new EventHandler<DataGridPreparingCellForEditEventArgs>(grid1_PreparingCellForEdit);
            grid1.CurrentCellChanged += new EventHandler<EventArgs>(grid1_CurrentCellChanged);
        }

        void grid1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (column != null && row != null)
            {
                TextBlock textBlock = (column.GetCellContent(row) as TextBlock);
                textBlock.Foreground = new SolidColorBrush(Colors.Blue);
                row = null;
                column = null;
            }
        }

        DataGridRow row;
        DataGridColumn column;
        void grid1_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
        {
            column = e.Column;
            row = e.Row;          
        }     

When the cell is getting ready to be edited we save the row and column and when the current cell changes we get the CellContent (usually a TextBlock, unless you are showing something different or have a CellTemplate)  and change the Foreground. Finally reset our variables

Make a Comment

Make A Comment: ( 1 so far )

blockquote and a tags work here.

One Response to “Changing Foreground of cells that are modified in DataGrid”

RSS Feed for Lee’s corner Comments RSS Feed

[...] Lee’s take on Changing Foreground of cells that are modified in DataGrid [...]


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...