In my first Silverlight application, I wrote a program that sends custom search strings to the Flickr REST API and displays the results in a DataGrid . The specified grid is defined as follows:
<data:DataGrid x:Name="PhotoGrid" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="Photo Title" Binding="{Binding Title}" CanUserSort="True" CanUserReorder="True" CanUserResize="True" IsReadOnly="True" /> <data:DataGridTemplateColumn Header="Photo" SortMemberPath="ImageUrl"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> <TextBlock Text="Click here to show image" MouseLeftButtonUp="ShowPhoto"/> <Image Visibility="Collapsed" MouseLeftButtonUp="HidePhoto"/> </StackPanel> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid>
This is a simple two-column table. The first column contains the name of the photo, and the second contains the text "Click here to show image." It clicks the ShowPhoto() call, which updates the Image element Source property with the BitmapImage obtained from the Flickr photo URI and sets the visibility of the image to Visible . Clicking on the image thus hides it again. All of this was easy to implement and works great.
But whenever I click on one of the column headings to sort by that column, the cells that I updated in this way do not change. The rest of the DataGrid runs and updates accordingly, but these cells are left behind, separated from the rest of their row. This is very strange, and not at all what I want.
What am I doing wrong? Do I have to refresh the DataGrid somehow in response to the sort event, and if so, how? Or, if I don't suppose that I should be randomly linked to the contents of the grid, what is the right way to get the behavior I want?
source share