2010-12-17 45 views
0

我当前使用以下方法来设置我的行背景的颜色。C#/ WPF - DataGrid - 超时后更新元素颜色

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

的问题是,这种转换器只要求使用新值的DataRow人口。我真正需要的是某种回调,在某段时间后改变颜色或定期重新评估转换器。

颜色更新不一定是即时的,只需几秒钟。如果我对每一行都有一个回调函数,那么我需要尽可能多的线程来匹配(它们被创建并因此在不同的时间到期(它会改变它们的颜色)),大约有1000行,这似乎不是一个有效的选项。

另一个选项是定期轮询一个线程上的行,并在每次迭代中重新评估转换器(每隔5秒?)。我认为这可能是要走的路,但我不知道如何去WPF中去做。

也许还有另一种方法或内置支持这样的任务?

提前致谢!

回答

0

应尽可能得到DataRow中的BindingExpression和简单的调用UpdateSource/UpdateTarget手动多次,因为你需要...

BindingExpression binding = Control.GetBindingExpression(DataRow.BackgroundProperty) 
binding.UpdateSource; 

不要忘记也改变了绑定UpdateSourceTrigger财产。