<p>I'm currently playing with the Silverlight(Beta 2) Datagrid control.  Before I wired up the SelectionChanged event, the grid would sort perfectly by clicking on the header.  Now, when the grid is clicked, it will fire the SelectionChanged event when I click the header to sort.  Is there any way around this?</p>

<p>In a semi-related topic, I'd like to have the SelectionChanged event fire when I click on an already selected item (so that I can have a pop-up occur to allow the user to edit the selected value).  Right now, you have to click on a different value and then back to the value you wanted in order for it to pop up.  Is there another way?  </p>

<p>Included is my code. </p>

<p>The Page:</p>

<pre><code>&lt;UserControl x:Class="WebServicesApp.Page"
xmlns="http://schemas.microsoft.com/client/2007" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
Width="1280" Height="1024" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"&gt;
&lt;Grid x:Name="LayoutRoot" Background="White"&gt;
    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition /&gt;
        &lt;RowDefinition /&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;StackPanel Grid.Row="0" x:Name="OurStack" Orientation="Vertical" Margin="5,5,5,5"&gt;
        &lt;ContentControl VerticalAlignment="Center" HorizontalAlignment="Center"&gt;
            &lt;StackPanel x:Name="SearchStackPanel" Orientation="Horizontal" Margin="5,5,5,5"&gt;
                &lt;TextBlock x:Name="SearchEmail" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="Email Address:" Margin="5,5,5,5" /&gt;
                &lt;TextBox x:Name="InputText" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="150" Height="Auto" Margin="5,5,5,5"/&gt;
                &lt;Button x:Name="SearchButton" Content="Search" Click="CallServiceButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="Auto" Background="#FFAFAFAF" Margin="5,5,5,5"/&gt;
            &lt;/StackPanel&gt;               
        &lt;/ContentControl&gt;
&lt;Grid x:Name="DisplayRoot" Background="White" ShowGridLines="True"
                HorizontalAlignment="Center" VerticalAlignment="Center" MaxHeight="300" MinHeight="100" MaxWidth="800" MinWidth="200"
                ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"&gt;
&lt;data:DataGrid ItemsSource="{Binding ''}" CanUserReorderColumns="False" CanUserResizeColumns="False"  
                           AutoGenerateColumns="False" AlternatingRowBackground="#FFAFAFAF" SelectionMode="Single" 
                           HorizontalAlignment="Center"  VerticalAlignment="Center" Margin="5,5,5,5" x:Name="IncidentGrid" SelectionChanged="IncidentGrid_SelectionChanged"&gt;
                &lt;data:DataGrid.Columns&gt;
                    &lt;data:DataGridTextColumn DisplayMemberBinding="{Binding Address}" Header="Email Address" IsReadOnly="True"  /&gt; &lt;!--Width="150"--&gt;
                    &lt;data:DataGridTextColumn DisplayMemberBinding="{Binding whereClause}" Header="Where Clause" IsReadOnly="True"  /&gt; &lt;!--Width="500"--&gt; 
                    &lt;data:DataGridTextColumn DisplayMemberBinding="{Binding Enabled}" Header="Enabled" IsReadOnly="True"  /&gt;
&lt;/data:DataGrid.Columns&gt;
            &lt;/data:DataGrid&gt;
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
    &lt;Grid x:Name="EditPersonPopupGrid" Visibility="Collapsed"&gt;
        &lt;Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.765" Fill="#FF8A8A8A" /&gt;
        &lt;Border CornerRadius="30" Background="#FF2D1DCC" Width="700" Height="400" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1,1,1,1" BorderBrush="#FF000000"&gt;
            &lt;StackPanel x:Name="EditPersonStackPanel" Orientation="Vertical" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="650" &gt;
                &lt;ContentControl&gt;
                    &lt;StackPanel x:Name="EmailEditStackPanel" Orientation="Horizontal"&gt;
                        &lt;TextBlock Text="Email Address:" Width="200" Margin="5,0,5,0" /&gt;
                        &lt;TextBox x:Name="EmailPopupTextBox" Width="200" /&gt;
                    &lt;/StackPanel&gt;
                &lt;/ContentControl&gt;
                &lt;ContentControl&gt;
                    &lt;StackPanel x:Name="AppliesToDropdownStackPanel" Orientation="Horizontal" Margin="2,2,2,0"&gt;
                        &lt;TextBlock Text="Don't send when update was done by:" /&gt;
                        &lt;StackPanel Orientation="Vertical" MaxHeight="275" MaxWidth="350" &gt;
                            &lt;TextBlock x:Name="SelectedItemTextBlock" TextAlignment="Right" Width="200" Margin="5,0,5,0" /&gt;
                            &lt;Grid x:Name="UserDropDownGrid" MaxHeight="75" MaxWidth="200" Visibility="Collapsed" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden" &gt;
                                &lt;Rectangle Fill="White" /&gt;
                                &lt;Border Background="White"&gt;
                                    &lt;ListBox x:Name="UsersListBox" SelectionChanged="UsersListBox_SelectionChanged" ItemsSource="{Binding UserID}" /&gt;
                                &lt;/Border&gt;                                       
                            &lt;/Grid&gt;
                        &lt;/StackPanel&gt;
                        &lt;Button x:Name="DropDownButton" Click="DropDownButton_Click" VerticalAlignment="Top" Width="25" Height="25"&gt;
                            &lt;Path Height="10" Width="10" Fill="#FF000000" Stretch="Fill" Stroke="#FF000000" Data="M514.66669,354 L542.16669,354 L527.74988,368.41684 z" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1,1,1,1"/&gt;
                        &lt;/Button&gt;
                    &lt;/StackPanel&gt;
                &lt;/ContentControl&gt;
                &lt;TextBlock Text="Where Clause Condition:" /&gt;
                &lt;TextBox x:Name="WhereClauseTextBox" Height="200" Width="800" AcceptsReturn="True" TextWrapping="Wrap" /&gt;
                &lt;ContentControl&gt;
                    &lt;StackPanel Orientation="Vertical"&gt;
                        &lt;StackPanel Orientation="Horizontal"&gt;
                            &lt;Button x:Name="TestConditionButton" Content="Test Condition" Margin="5,5,5,5" Click="TestConditionButton_Click" /&gt;
                            &lt;Button x:Name="Save" Content="Save" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Save_Click" /&gt;
                            &lt;Button x:Name="Cancel" Content="Cancel" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Cancel_Click" /&gt;                       
                        &lt;/StackPanel&gt;   
                        &lt;TextBlock x:Name="TestContitionResults" Visibility="Collapsed" /&gt;
                    &lt;/StackPanel&gt;
                &lt;/ContentControl&gt;
            &lt;/StackPanel&gt;               
        &lt;/Border&gt;               
    &lt;/Grid&gt;     
&lt;/Grid&gt;
</code></pre>

<p></p>

<p>And the call that occurs when the grid's selection is changed:</p>

<pre><code>Private Sub IncidentGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If mFirstTime Then
        mFirstTime = False
    Else
        Dim data As SimpleASMX.EMailMonitor = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor)
        Dim selectedGridItem As SimpleASMX.EMailMonitor = Nothing
        If IncidentGrid.SelectedItem IsNot Nothing Then
            selectedGridItem = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor)
            EmailPopupTextBox.Text = selectedGridItem.Address
            SelectedItemTextBlock.Text = selectedGridItem.AppliesToUserID
            WhereClauseTextBox.Text = selectedGridItem.whereClause
            IncidentGrid.SelectedIndex = mEmailMonitorData.IndexOf(selectedGridItem)
        End If
        If IncidentGrid.SelectedIndex &gt; -1 Then
            EditPersonPopupGrid.Visibility = Windows.Visibility.Visible
        Else
            EditPersonPopupGrid.Visibility = Windows.Visibility.Collapsed
        End If
    End If
End Sub
</code></pre>

<p>Sorry if my code is atrocious, I'm still learning Silverlight.</p>

[{'answer_id': 74877, 'author': 'Senkwe', 'author_id': 6419, 'author_profile': 'https://Stackoverflow.com/users/6419', 'pm_score': 3, 'selected': True, 'text': '<p>That looks like a Silverlight bug to me. I\'ve just tried it and what\'s happening on my end is that the <strong>SelectionChanged</strong> event fires twice when you click the column header and to make matters worse, the index of the selected item doesn\'t stay synched with the currently selected item.</p>\n\n<p>I\'d suggest you work your way around it by using the knowledge that the first time SelectionChanged fires, the value of the datagrid\'s <strong>SelectedItem</strong> property will be <strong>null</strong></p>\n\n<p>Here\'s some sample code that at least lives with the issue. Your <strong>SelectionChanged</strong> logic can go in the <strong>if</strong> clause.</p>\n\n<pre><code>public partial class Page : UserControl\n{\n    private Person _currentSelectedPerson;\n\n    public Page()\n    {\n        InitializeComponent();\n\n        List&lt;Person&gt; persons = new List&lt;Person&gt;();\n        persons.Add(new Person() { Age = 5, Name = "Tom" });\n        persons.Add(new Person() { Age = 3, Name = "Lisa" });\n        persons.Add(new Person() { Age = 4, Name = "Sam" });\n\n        dg.ItemsSource = persons;\n    }       \n\n    private void SelectionChanged(object sender, EventArgs e)\n    {\n        DataGrid grid = sender as DataGrid;\n        if (grid.SelectedItem != null)\n        {\n            _currentSelectedPerson = grid.SelectedItem as Person;\n        }\n        else\n        {\n            grid.SelectedItem = _currentSelectedPerson;\n        }\n    }\n }\n\npublic class Person\n{\n    public string Name { get; set; }\n    public int Age { get; set; }\n}\n</code></pre>\n'}, {'answer_id': 76586, 'author': 'Rob', 'author_id': 12413, 'author_profile': 'https://Stackoverflow.com/users/12413', 'pm_score': 0, 'selected': False, 'text': "<p>This worked, but now if I sort twice, on the first one it sorts, and then does the popup as the first selected item of the grid .  If I close the popup grid, and then try to sort a second time, it stack overflows, and crashes firefox out.</p>\n\n<p>I'm thinking I may need to rethink working in silverlight until the system gets a bit more stable.</p>\n\n<p>Thanks for the answer Hovito!</p>\n"}, {'answer_id': 498801, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Frozen Columns in Silverlight DataGrid..</p>\n\n<p><a href="http://dotnetdreamer.wordpress.com/2009/01/31/silverlight-2-datagrid-frozen-columns/" rel="nofollow noreferrer">http://dotnetdreamer.wordpress.com/2009/01/31/silverlight-2-datagrid-frozen-columns/</a></p>\n'}, {'answer_id': 592048, 'author': 'Matthew Timbs', 'author_id': 21343, 'author_profile': 'https://Stackoverflow.com/users/21343', 'pm_score': 1, 'selected': False, 'text': '<p>There\'s  a bugfix for the first issue you mentioned (selection changed event getting fired on resort).</p>\n\n<p>See the following URL for Microsoft\'s patch:</p>\n\n<p><a href="http://www.microsoft.com/downloads/details.aspx?familyid=084A1BB2-0078-4009-94EE-E659C6409DB0&amp;displaylang=en" rel="nofollow noreferrer">http://www.microsoft.com/downloads/details.aspx?familyid=084A1BB2-0078-4009-94EE-E659C6409DB0&amp;displaylang=en</a></p>\n'}]
['https://Stackoverflow.com/questions/74461', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12413/']