我的Silverlight应用程序中有一个DataGrid,我希望在该列排序时突出显示整个列。
它在概念上与前一个问题相似:Silverlight DataGrid: Highlight an entire column。
有什么想法吗?我想可能会将一个行为附加到LayoutUpdated事件并检查一个单元格以查看它是否在一个已排序的列中?
答案 0 :(得分:3)
你必须要做几件事才能做你想做的事。最大的问题是找出列何时排序要比它应该更难。我找到的唯一方法是确定列已排序以使DataGrid.ItemsSource
成为PagedCollectionView并在其CollectionChanged
属性上侦听SortDescriptions
事件。如果对ItemsSource使用PagedCollectionView
,并且所有可以排序的列都是DataGridBoundColumn
(实际上除了TemplateColumn以外的任何预备版),那么这种行为应该与您想要做的非常接近。
public class DataGridSortedColumnHighlightBehavior : Behavior<DataGrid>
{
private List<DataGridRow> rows = new List<DataGridRow>();
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.LoadingRow += AssociatedObject_LoadingRow;
AssociatedObject.UnloadingRow += AssociatedObject_UnloadingRow;
if (AssociatedObject.ItemsSource == null)
{
AssociatedObject.LayoutUpdated += AssociatedObject_LayoutUpdated;
}
else
{
var collection =
((AssociatedObject.ItemsSource as PagedCollectionView).SortDescriptions as INotifyCollectionChanged);
collection.CollectionChanged += DataGridSortedColumnHighlightBehavior_CollectionChanged;
}
}
void AssociatedObject_UnloadingRow(object sender, DataGridRowEventArgs e)
{
rows.Remove(e.Row);
}
void AssociatedObject_LoadingRow(object sender, DataGridRowEventArgs e)
{
rows.Add(e.Row);
}
private void AssociatedObject_LayoutUpdated(object sender, EventArgs e)
{
if (AssociatedObject.ItemsSource != null)
{
AssociatedObject.LayoutUpdated -= AssociatedObject_LayoutUpdated;
var collection =
((AssociatedObject.ItemsSource as PagedCollectionView).SortDescriptions as INotifyCollectionChanged);
collection.CollectionChanged += DataGridSortedColumnHighlightBehavior_CollectionChanged;
}
}
void DataGridSortedColumnHighlightBehavior_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(e.NewItems != null)
{
foreach(SortDescription sortDesc in e.NewItems)
{
foreach(var column in AssociatedObject.Columns)
{
var boundColumn = column as DataGridBoundColumn;
if (boundColumn == null)
continue;
if (boundColumn.Binding.Path.Path == sortDesc.PropertyName)
{
foreach (var row in rows)
ColorCell(column,row,Colors.Red);
}
}
}
}
if(e.OldItems != null)
{
foreach(SortDescription sortDesc in e.OldItems)
{
foreach(var column in AssociatedObject.Columns)
{
var boundColumn = column as DataGridBoundColumn;
if (boundColumn == null)
continue;
if (boundColumn.Binding.Path.Path == sortDesc.PropertyName)
{
foreach (var row in rows)
ColorCell(column,row,Colors.White);
}
}
}
}
}
private void ColorCell(DataGridColumn column, DataGridRow item, Color color )
{
var content = column.GetCellContent(item);
if (content == null)
return;
var parent = GetParent<DataGridCell>(content);
if (parent != null)
parent.Background = new SolidColorBrush(color);
}
public static T GetParent<T>(DependencyObject source)
where T : DependencyObject
{
DependencyObject parent = VisualTreeHelper.GetParent(source);
while (parent != null && !typeof(T).IsAssignableFrom(parent.GetType()))
{
parent = VisualTreeHelper.GetParent(parent);
}
return (T)parent;
}
}
答案 1 :(得分:0)