快问 .
我有一个DataGrid,它通过SQL-Dependency自动更新 . 我设置了一个触发器,可以根据几个值(红色或绿色)将BackgroundProperty更改为一种颜色 . 我真的希望在我的触发器中使用eventsetter .
我的问题是:当触发器改变背景时,哪个事件处理程序触发?我已经尝试过DependencyPropertyDescriptor - 它在检查itemssource时发生了变化,但是没有DataGridRow.BackgroundProperty或Control.BackgroundProperty(只是不触发)和我的dependencyproperty一样
var desc = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(Control));
desc.AddValueChanged(MyDataGrid,MyEventHandler);
我的活动是这样的:
DataGrid mygrid = sender as DataGrid;
var itemsSource = mygrid.ItemsSource as IEnumerable;
if (null != itemsSource)
{
foreach (var item in itemsSource)
{
var row = mygrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (null != row)
{
if (Equals(Brushes.Red, row.Background))
{
//Do whatever
}
}
}
}
但是行总是为空 - 这是为什么?编辑:它返回null,因为它是虚拟化的,不是“在视图中” .
我希望能够创建一个事件处理程序来与我的数据触发器中的事件处理器一起使用
先感谢您 .
1 回答
@Sheridan建议的是你已经知道什么时候应该改变背景颜色所以只需使用那些数据并做你想要的额外工作,
但是如果你只是必须创建一个回调更改后面的回调,那么你可以使用DependencyPropertyDescriptor添加更改的处理程序:
var desc = DependencyPropertyDescriptor.FromProperty(Control.BackGroundProperty,typeof(Control)); desc.AddValueChanged(此,yourEventHandler);