8,756
社区成员




private List<DataGridRow> _myDataGridRows = new List<DataGridRow>();
void myDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
_myDataGridRows.Add(e.Row);
}
void myDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.ResetRowsColor();
var possibleSelection = _myDataGridRows.Where(w => w.GetIndex() == myDataGrid.SelectedIndex - 1 || w.GetIndex() == myDataGrid.SelectedIndex + 2)
.ToList();
if (possibleSelection.Count == 2)
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Red);
this.SetRowColor(possibleSelection.ElementAt(1), Colors.Blue);
}
else if (possibleSelection.Count == 1)
{
if (possibleSelection.ElementAt(0).GetIndex() < myDataGrid.SelectedIndex)
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Red);
}
else
{
this.SetRowColor(possibleSelection.ElementAt(0), Colors.Blue);
}
}
}
public void ResetRowsColor()
{
foreach (var row in this._myDataGridRows)
{
this.SetRowColor(row, Colors.Transparent);
}
}
private void SetRowColor(DataGridRow row, Color color)
{
Grid rootGrid = MyVisualTreeHelper.SearchFrameworkElement(row, "Root") as Grid;
if (rootGrid != null)
{
rootGrid.Background = new SolidColorBrush(color);
}
}
public FrameworkElement SearchFrameworkElement(FrameworkElement parentFrameworkElement, string childFrameworkElementNameToSearch)
{
FrameworkElement childFrameworkElementFound = null;
SearchFrameworkElement(parentFrameworkElement, ref childFrameworkElementFound, childFrameworkElementNameToSearch);
return childFrameworkElementFound;
}
private void SearchFrameworkElement(FrameworkElement parentFrameworkElement, ref FrameworkElement childFrameworkElementToFind, string childFrameworkElementName)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(parentFrameworkElement);
if (childrenCount > 0)
{
FrameworkElement childFrameworkElement = null;
for (int i = 0; i < childrenCount; i++)
{
childFrameworkElement = (FrameworkElement)VisualTreeHelper.GetChild(parentFrameworkElement, i);
if (childFrameworkElement != null && childFrameworkElement.Name.Equals(childFrameworkElementName))
{
childFrameworkElementToFind = childFrameworkElement;
return;
}
SearchFrameworkElement(childFrameworkElement, ref childFrameworkElementToFind, childFrameworkElementName);
}
}
}
Brush redBrush = new SolidColorBrush(Colors.Red);
Brush blackBrush = new SolidColorBrush(Colors.Black);
grid.BackGroud=blackBrush;
if (cell.Background == new SolidColorBrush(Colors.Red)) { };