PreviewMouseDown 메서드 here에 대한 몇 가지 예가 있습니다.
일반적으로 DataGrid.SelectedItem을 DataGrid의 SelectionChanged 핸들러 내에서 원래 값으로 되 돌리는 것은 예상대로 작동하지 않습니다. Dispatcher에게 나중에 스케줄을달라고 요청함으로써 취소를 연기하는 모든 코드 예제가 있습니다.
DataGrid에 CellStyle이 있습니까? 나를 위해,이 일을 다음
XAML :
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="DarkSlateBlue"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
코드 숨김 :
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
object x = e.AddedItems[0];
if (x is MyObjectType && x != myViewModel.CurrentItem &&
myViewModel.ShouldNotDeselectCurrentItem())
{
// this will actually revert the SelectedItem correctly, but it won't highlight the correct (old) row.
this.MyDataGrid.SelectedItem = null;
this.MyDataGrid.SelectedItem = myViewModel.CurrentItem;
}
}
}
이 점은 SelectedCellsChanged 이벤트가 인 selectionchanged 이벤트 후 해고이었다 - 그리고 특히, 설정이 SelectedItem이 읽기 전용 속성 인 SelectedCell을 올바르게 업데이트하지 않으므로 코드 숨김이 더 많이 발생합니다.
private void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
List<DataGridCellInfo> selectedCells = MyDataGrid.SelectedCells.ToList();
List<MyObjectType> wrongObjects = selectedCells.Select(cellInfo => cellInfo.Item as MyObjectType)
.Where (myObject => myObject != myViewModel.CurrentItem).Distinct().ToList();
if (wrongObjects.Count > 0)
{
MyDataGrid.UnselectAllCells();
MyDataGrid.SelectedItem = null;
MyDataGrid.SelectedItem = myViewModel.CurrentItem;
}
}
분명히 처리기는 데이터 그리드의 해당 이벤트에 연결되어야합니다.
예상대로 작동하고 원하는 경우 선택 변경을 적절히 취소했으며 깜박임을 생성하지 않았습니다.
전 제안은 완벽하게 작동했습니다 (후자는 매우 흥미로운 제안이지만 시도해 보았습니다). 감사합니다. – Mani5556