C# / WPF -DataGrid-超时后更新元素颜色
-
11-10-2019 - |
题
我目前使用以下方法设置行背景的颜色。
XAML
<Style TargetType="{x:Type xcdg:DataRow}">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource colorConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="IsSelected"/>
<Binding BindsDirectlyToSource="True" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
C#
public class ColourConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var isRowSelected = (bool)values[0];
var myInstance = (MyClass) values[1];
// Return default
if (isRowSelected || myInstance == null)
return DependencyProperty.UnsetValue;
// Get the check for the current field
return GetColour(myInstance) ?? DependencyProperty.UnsetValue;
}
private static SolidColorBrush GetColour(MyClass myInstance)
{
if (heartbeat == null)
{
return null;
}
// Is it more two minutes old?
return (myInstance.CreatedDateTime + TimeSpan.FromMinutes(2) < Clock.UtcNow())
? Brushes.LightPink
: Brushes.LightGreen;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException(this.GetType().Name + " cannot convert back");
}
}
问题在于,该转换器仅在具有新值的数据群中调用。我真正需要的是某种回调,以在一定时间后更改颜色或定期重新评估转换器。
颜色更新不必在几秒钟内即时即时。如果我对每一行都有回调,那么我需要匹配尽可能多的线程(它们是创建的,因此在不同的时间到期(会改变其颜色)),而约有1000行,这似乎不是一个有效的选项。
另一个选项是定期轮询一个线程上的行,并在每次迭代(每5秒?)上重新评估转换器。我认为这可能是要走的路,但我不知道该如何在WPF中进行。
也许还有另一种方法或对此类任务的支持?
提前致谢!
解决方案
应该有可能从dataRow获取绑定表达,只需按照您需要手动多次调用updateSource/updateTarget ...
BindingExpression binding = Control.GetBindingExpression(DataRow.BackgroundProperty)
binding.UpdateSource;
不要忘记也更改 UpdateSourCetrigger 绑定上的属性。
不隶属于 StackOverflow