private SolidColorBrush DataGridRowSelectedForegroundBrush { get; set; } = new(Colors.LightGreen);
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not DataGrid dataGrid)
{
return;
}
// DataGridRow has an "IsSelected" property but it's internal.
// We can use Reflection to get this property.
Type dataGridRowType = typeof(DataGridRow);
PropertyInfo? isSelectedProperty = dataGridRowType.GetProperty("IsSelected", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (DataGridRow dataGridRow in dataGrid.FindDescendants().OfType<DataGridRow>())
{
if (isSelectedProperty?.GetValue(dataGridRow) is bool isSelected &&
isSelected is true)
{
dataGridRow.Foreground = DataGridRowSelectedForegroundBrush;
continue;
}
dataGridRow.Foreground = dataGrid.Foreground;
}
}
1条答案
按热度按时间nlejzf6q1#
您可以使用
DataGrid
的SelectionChanged
事件:字符串
顺便说一句,我正在使用CommunityToolkit.WinUI.UINuGet包来使用
FindDescendants()
。