I am using Silverlight 4 and have a datagrid that allows users to sort the rows. On the sort column event the SelectionChanged event gets fired and the initial first row in the datagrid is selected. Is there anyway to not have the SelectionChanged event fired or is there away to have an onSort event to set the selectedItem to null?
1
votes
2 Answers
2
votes
This appears to be an issue that cannot be easily overcome by adding logic to the SelectionChanged event. What I'm doing is removing all code from this event and putting it in the MouseLeftUpButton event of each row. Here is the code for that...
Private Sub dgrd_LoadingRow(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridRowEventArgs)
AddHandler e.Row.MouseLeftButtonUp, AddressOf ClientGrid_SelectRow
End Sub
Private Sub ClientGrid_SelectRow(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim SelectedRow As DataGridRow = CType(sender, DataGridRow)
//SelectedRow.DataContext is equivlent to e.AddedItems(0) in the SelectionChanged Event
//Add the SelectionChanged logic here
End Sub
0
votes
Adding a row handler didn't work for me... but this did:
private bool _binding = false;
private void data_grid_LoadingRow(object sender, DataGridRowEventArgs e)
{
_binding = true;
}
private void data_grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_binding)
{
data_grid.SelectedItem = null;
_binding = false;
}
if (data_grid.SelectedItem==null) return;
// do something with selected item
}
Thanks to jasonxz found here http://forums.silverlight.net/p/18215/61135.aspx