問題はかなり簡単です。私のDataGridは、基本的に文字列でいっぱいのオブジェクトのリストである私のItemSource(bindingList)からいっぱいです。DataGridの行選択の保存と復元
私のコードのこの特定の部分で、私は自分のbindingListを更新する必要があります。 残念ながら、更新されると、DataGridで行われたすべてのユーザー行の選択が消えます。
これは、私が是正したいと思っている不便さです。そのため、ユーザーがbindingListを更新するボタンをクリックしたときに、ユーザーがさらに変更したい場合に備えて、選択内容が保存されます。
コード今すぐ右:
//Save DataGrid Row Selections
bindingList[1] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip });
dataGrid.ItemSource = bindingList;
//Restore DataGrid Row Selections
EDIT: より広い範囲の要求に応じて:
private void Y_Incre_Button_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItems.Count != 0)
{
string colonum;
string xpos;
string ypos;
string descrip;
for (int i = 0; i < bindingList.Count; i++)
{
int selectionIndex = dataGrid.SelectedIndex;
if (selectionIndex > -1)
{
var curItem = bindingList[selectionIndex];
int yNum = int.Parse(curItem.ycoord);
int yNum2 = (yNum + 10);
colonum = curItem.columnnumber;
xpos = curItem.xcoord;
ypos = yNum2.ToString();
descrip = curItem.description;
//Save DataGrid Row Selections
bindingList[selectionIndex] = (new ItemClass() { columnnumber = colonum, xcoord = xpos, ycoord = ypos, description = descrip });
//Restore DataGrid Row Selections
}
}
}
else
{
MessageBox.Show("No Rows Selected");
}
}
は、あなたがのObservableCollectionにアイテムを、その後、のObservableCollectionにあなたのItemSourceを設定する挿入/削除するには、結合(代わりのコレクションを交換し、適切なXAMLを使用した場合 – aguertin
(その15行または周囲の)してくださいもう少しコードを示すことができました)あなたが望むものを達成するかもしれません。 – slugster
私はアイテムを挿入または削除するのではなく、選択したアイテムを(それを置き換えることによって)更新するだけです。それはおそらくObservableCollectionで別々に行われますか? – Mercender