2016-05-28 13 views
0

私は観測可能なコレクションを持つDataGridを持っています。observablecollectionを使用したC#wpfデータグリッド。ユーザーがソートした後、私はobservablecollectionの選択インデックスを取得できません

<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"/> 

そして

public ObservableCollection<SavedMemorySWL> SavedMemorySWLObservableCollection = new ObservableCollection<SavedMemorySWL>(); 
    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; 
    } 

とのObservableCollectionに行を追加した後

...

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

ので、問題は、私は後で、その後、ユーザーが列の並べ替えを許可した場合にI選択された行を取得し、ObservableCollectionの行と一致しません。

//int row = DataGridMemoryTable.SelectedIndex; 
     int row = DataGridMemoryTable.Items.IndexOf(DataGridMemoryTable.SelectedItem); //.SelectedIndex; 

ソート後、ユーザーはボタンをクリックして、選択した行で何かを実行します。それが一番上の行であれば、selectedindexは0です。しかし、ObservableCollectionの値はソートされておらず、間違ったObservableCollection行でこの操作を行います。

私の現在のアプローチは、ユーザーによる並べ替えを許可しないことです。

誰かがのObservableCollectionにインデックスにselectedIndexのを変換する方法のアイデアを持っていますか?

+0

ユーザーが列ではなく、DataGrid内のデータのObservableCollectionを並べ替えるソートします。 – jdweng

+0

データグリッドを手動で更新する理由が混乱しています。 ObservableCollectionは、オブジェクトが変更されたときにバインドされているオブジェクトに自動的に通知します。あなたのDataGridのバインディング式は単に{Binding}です。つまり、親DataContextを使用しています - あなたのObservableCollectionの親DataContextですか?それをする必要があります - あなたがそれを行う場合、更新はそれ自身を世話する必要があり、この問題はなくなるはずです。 – Travis

+0

jdweng。いいアイデアですね。これは余分な作業/コーディングですが、それは実行可能な解決策です。 "Sorted"イベントは、DatGridSortingEventArgs eを渡します。 e.Column.SortMemberPathおよびe.Column.SortDirectionは、ユーザーが開始したソートに関する情報を提供します。トラビス。はい、ユーザーがデータグリッドを変更すると、ObservableCollectionが自動的に変更されます。ただし、ユーザーがデータグリッドでソートした列ソートは、ObservableCollectionに反映されていないようです。 – Howard

答えて

0

私はこの質問をする前に検索しましたが、さらに検索した後、私は答えを見つけました。

Sort ObservableCollection bound to DataGrid in MVVM

答えは、それが不可能であるということです。データグリッドのソート

ユーザーのObservableCollectionで下線データを変更することはありません。これを達成する方法はありません。明らかにマイクロソフトでは、アンダーラインデータもソートされることを誰も期待していなかったので、これを達成するためのプロパティ設定やメソッドはありません。

ユーザーソートは、ICollectionViewのデータにのみ影響します。しかし、ICollectionViewをそのスレッドの2番のポストに示すように見てコピーする方法があります。

public ICollectionView myCVSWL { get; set; } 
internal SavedMemorySWL[] MemoriesSWL = new SavedMemorySWL[MAX_ROWS_SWL]; 
. 
. 
. 
Global.MW.DataGridMemoryTable.Dispatcher.Invoke(new Action(() => 
      { 
       if (Global.MW != null) 
       { 
        myCVSWL = CollectionViewSource.GetDefaultView(SavedMemorySWLObservableCollection); 
        Global.MW.DataGridMemoryTable.ItemsSource = myCVSWL; // SavedMemorySWLObservableCollection; 
        Global.MW.DataGridMemoryTable.Items.Refresh(); 
        Global.MW.textBoxFileName.Text = memoryRows.FileName; 
       } 
      })); 
. 
. 
. 





         int k = 0; 
        foreach (var item in myCVSWL.OfType<SavedMemorySWL>()) 
        { 
         memoryRows.MemoriesSWL[k].Frequency = item.Frequency; 
         memoryRows.MemoriesSWL[k].Time = item.Time; 
         memoryRows.MemoriesSWL[k].Days = item.Days; 
         memoryRows.MemoriesSWL[k].ITU_Station = item.ITU_Station; 
         memoryRows.MemoriesSWL[k].Language_Target_Remarks = item.Language_Target_Remarks; 
         memoryRows.MemoriesSWL[k].Program_Start_Stop = item.Program_Start_Stop; 
         memoryRows.MemoriesSWL[k].ATAUTOinductor = item.ATAUTOinductor; 
         memoryRows.MemoriesSWL[k].ATAUTOcapacitor = item.ATAUTOcapacitor; 
         memoryRows.MemoriesSWL[k].Antenna = item.Antenna; 
         memoryRows.MemoriesSWL[k].Scan = item.Scan; 
         memoryRows.MemoriesSWL[k].AT200PCinductor = item.AT200PCinductor; 
         memoryRows.MemoriesSWL[k].AT200PCcapacitor = item.AT200PCcapacitor; 
         k++; 
        } 

これをいくつか考えて助けてくれた人に感謝します。

よろしく、 ハワード

+0

ICollectionViewがINotifyPropertyChangedをサポートしていないように見えるため、これらの変更をすべて無効にして、データグリッド列ソートを無効にしました。私はJava/SwingとVS/WPFの両方でPITAになるようにテーブルを扱っています。 – Howard

関連する問題