One solution could be MultiBinding -- the first property would be the existing LastContact
, and the second property would be a new one on your view model called "CurrentTime" or something. Run a timer that updates the CurrentTime property every X seconds, and that should trigger an update on the binding.
Something like this:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource multiDateConverter}">
<Binding Path="LastContact" />
<Binding Path="DataContext.CurrentTime"
RelativeSource="{RelativeSource AncestorType=DataGrid}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Here you would need to change your date converter to be an IMultiValueConverter
. Just an example:
public class MultiDateConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (!(values.First() is DateTime))
return DependencyProperty.UnsetValue;
return string.Format("{0:N1} seconds ago", DateTime.Now.Subtract((DateTime)values.First()).TotalSeconds);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The last piece is just to run a dispatcher timer to update the new "CurrentTime" property:
const int refreshRateInSeconds = 1;
var timer = new DispatcherTimer();
timer.Tick += (sender, args) => CurrentTime = DateTime.Now;
timer.Interval = TimeSpan.FromSeconds(refreshRateInSeconds);
timer.Start();