Silverlight Datagrid:突出显示整个列? [英] Silverlight Datagrid: Highlight an entire Column?

查看:201
本文介绍了Silverlight Datagrid:突出显示整个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 DataGrid ,并且希望在选择该列中的任何单元格时突出显示整个列。



例如,给定此网格(其中 [] 表示单元格):

  [] [] [] 
[] [] []
[ code>



如果我选择一个单元格,像这样

  [] [selected] [] 
[] [] []
[] [] []


我想将该列中的所有单元格(包括所选单元格)突出显示(可以像更改背景颜色一样简单):

  [] [selected] [] 
[] [highlight] []
[ b

有简单的方法吗?
谢谢!

解决方案

这是行动的开始,应该指向正确的方向

  public class DataGridHighlightBehavior:Behavior< DataGrid> 
{
protected override void OnAttached()
{
base.OnAttached();

AssociatedObject.CurrentCellChanged + = AssociatedObject_CurrentCellChanged;
}

void AssociatedObject_CurrentCellChanged(object sender,EventArgs e)
{
foreach(对象i在AssociatedObject.ItemsSource中)
{
var item = AssociatedObject.CurrentColumn.GetCellContent(i);
if(item == null)
return;
var parent = GetParent< DataGridCell>(item);
if(parent!= null)
parent.Background = new SolidColorBrush(Colors.Red);
}
}

public static T GetParent< T>(DependencyObject source)
其中T:DependencyObject
{
DependencyObject parent = VisualTreeHelper .GetParent(source);
while(parent!= null&&!typeof(T).IsAssignableFrom(parent.GetType()))
{
parent = VisualTreeHelper.GetParent
}
return(T)parent;
}
}

您需要添加代码才能更改旧单元格恢复到正常状态。我最初的想法是修改他们目前的视觉状态,所以他们显示选定,但不记得如何(如果你可以)从类外做。


I have a DataGrid in my Silverlight application, and would like to "highlight" an entire column when any cell in that column is selected.

E.g., given this grid (where "[ ]" represents a cell):

[     ][     ][     ]
[     ][     ][     ]
[     ][     ][     ]

If I select a cell, like this

[     ][ selected ][     ]
[     ][          ][     ]
[     ][          ][     ]

I would like all the cells in that column, including the selected cell, to be "highlighted" (can be as simple as just changing the background color):

[     ][  selected   ][     ]
[     ][ highlighted ][     ]
[     ][ highlighted ][     ]

Is there an easy way to do this? Thanks!

解决方案

Here is the start of behavior that should point you in the right direction

    public class DataGridHighlightBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.CurrentCellChanged += AssociatedObject_CurrentCellChanged;
    }

    void AssociatedObject_CurrentCellChanged(object sender, EventArgs e)
    {
        foreach (object i in AssociatedObject.ItemsSource)
        {
            var item = AssociatedObject.CurrentColumn.GetCellContent(i);
            if (item == null)
                return;
            var parent = GetParent<DataGridCell>(item);
            if (parent != null)
                parent.Background = new SolidColorBrush(Colors.Red);
        }
    }

    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;
    }
}

You will need to add code to change the old cells back to their normal state. My initial thought was to modify their current visual state so they show selected, but couldn't remember how (if you can) to do that from outside the class.

这篇关于Silverlight Datagrid:突出显示整个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆