gpt4 book ai didi

c# - Silverlight 数据网格 : Highlight an entire column when that column is sorted

转载 作者:行者123 更新时间:2023-11-30 22:43:23 28 4
gpt4 key购买 nike

我的 Silverlight 应用程序中有一个 DataGrid,我想在该列排序时突出显示该列。

它在概念上与上一个问题相似:Silverlight DataGrid: Highlight an entire column .

有什么想法吗?我想也许可以将行为附加到 LayoutUpdated 事件并检查单元格以查看它是否在排序列中?

最佳答案

要完成您想做的事情,您必须解决几个问题。最大的问题是找出列何时排序比它应该的要难得多。我发现识别列已排序以使 DataGrid.ItemsSource 成为 PagedCollectionView 并在其 SortDescriptions 上收听 CollectionChanged 事件的唯一方法> 属性(property)。如果您为 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;
}
}

关于c# - Silverlight 数据网格 : Highlight an entire column when that column is sorted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3970363/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com