C# wpf datagrid with observablecollection. After a user sorts, I can not get the selecctedindex of the observablecollection
<p>I have a datagrid with an observable collection.</p>

<pre><code> &lt;DataGrid Name="DataGridMemoryTable" AlternatingRowBackground="Beige" Canvas.Left="87" Canvas.Top="30" Width="500" Height="300" RowHeight="20" ColumnWidth="60"
                      SelectionMode="Single" CanUserSortColumns = "False" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="10" ItemsSource="{Binding}" 
                      CurrentCellChanged="DataGridMemoryTableCellChanged" Sorting="DataGridMemoryTable_Sorted"/&gt;
</code></pre>

<p>And</p>

<pre><code>public ObservableCollection&lt;SavedMemorySWL&gt; SavedMemorySWLObservableCollection = new ObservableCollection&lt;SavedMemorySWL&gt;();
    public class SavedMemorySWL : INotifyPropertyChanged
    {
        public String Frequency { get; set; }
        public String Time { get; set; }
        public String Days { get; set; }
        public String ITU_Station { get; set; }
        public String Language_Target_Remarks { get; set; }
        public String Program_Start_Stop { get; set; }
        public String ATAUTOinductor { get; set; }
        public String ATAUTOcapacitor { get; set; }
        public String Antenna { get; set; }
        public Boolean Scan { get; set; }
        public String AT200PCinductor { get; set; }
        public String AT200PCcapacitor { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
</code></pre>

<p>and after adding rows into the ObservableCollection...</p>

<pre><code>Global.MW.DataGridMemoryTable.Dispatcher.Invoke(new Action(() =&gt;
            {
                if (Global.MW != null)
                {
                    Global.MW.DataGridMemoryTable.ItemsSource = SavedMemorySWLObservableCollection;
                    Global.MW.DataGridMemoryTable.Items.Refresh();
                    Global.MW.textBoxFileName.Text = memoryRows.FileName;
                }
            }));
</code></pre>

<p>So, the problem is that if I allow column sorting by the user, then later I get the selected row, and it does not match the row in the ObservableCollection.</p>

<pre><code>//int row = DataGridMemoryTable.SelectedIndex;
        int row = DataGridMemoryTable.Items.IndexOf(DataGridMemoryTable.SelectedItem);    //.SelectedIndex;
</code></pre>

<p>So, after sorting the user clicks a button to do something with his selected row.  If it is the top row, the selectedindex is 0.  But the values in the ObservableCollection are not sorted and I do this operation on the wrong ObservableCollection row.</p>

<p>My current approach is to not permit sorting by the user.</p>

<p>Does somebody have an idea how to convert the selectedindex into an index into the ObservableCollection?</p>