我已經處理上選擇change.This隱藏的項目解決的問題是使用MVVM模式來實現。
private void HandleContentSelection(SelectionChangedEventArgs e)
{
if (e == null) return;
var dataGrid = e.Source as DataGrid;
if (dataGrid == null) return;
#region Update Grid Selection
if (e.AddedItems != null)
{
foreach (var item in e.AddedItems.Cast<IContent>())
{
if (!item.IsSelected)
{
item.IsSelected = true;
}
}
}
if (e.RemovedItems != null)
{
_contentsToBeUnselected.Clear();
// Handle Multiple selection
// Set hidden items IsSelected property to false
if (dataGrid.SelectedItems.Count > 0 && (e.AddedItems == null || e.AddedItems.Count == 0) && e.RemovedItems.Count > 1)
{
foreach (var content in dataGrid.Items.Cast<IContent>())
{
if (!dataGrid.SelectedItems.Contains(content))
{
_contentsToBeUnselected.Add(content);
}
}
}
// Handle Unselect All
// Set hidden items IsSelected property to false
else if (dataGrid.SelectedItems.Count == 0 && (e.AddedItems == null || e.AddedItems.Count == 0))
{
_contentsToBeUnselected.AddRange(dataGrid.Items.Cast<IContent>());
}
else
{
_contentsToBeUnselected.AddRange(e.RemovedItems.Cast<IContent>());
}
foreach (var item in _contentsToBeUnselected)
{
if (item.IsSelected)
{
// if bound data item still is selected, fix this
item.IsSelected = false;
}
}
}
e.Handled = true;
#endregion
}
這不是你的描述中的錯誤嗎? DataGrid.Select ** All()**只選擇一些特定的行嗎? – VDN
對不起,我感到困惑。我更新了我的問題。希望您能夠幫助我。謝謝! – Dyva
您是否嘗試在選擇特定的行之前取消選擇所有行?像'dataGridView.ClearSelection()';據我所知,用'row.IsSelected'你可以選擇任意數量的行。它不會取消選擇先前選擇的行:'for(int i = 0; i <10; i ++)rows [i] .isSelected = true'將選擇所有10行 – VDN