Reputation: 815
I have a listview like the picture below. As you can se I have one column called Time. I want to know how I can update the time automatically.
For example the time of Flight Code ABC123 should after 1 minute change from 3 minutes to 4 minutes.
Upvotes: 3
Views: 1571
Reputation: 6113
Edit wasn't happy with the idea of invalidating the binding every minute (that could be completely unfounded, and invalidating every minute is fine, I don't know), so came up with a 'purer' implementation that sets the content directly, left the original in for reference though:
You could look to do it through an AttachedBehaviour
, the below should be able to get you started:
public static class TimeAgoBehaviour {
static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
timer.Start();
return timer;
});
static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();
public static DateTime GetTimeAgo(ContentControl obj) {
return (DateTime)obj.GetValue(TimeAgoProperty);
}
public static void SetTimeAgo(ContentControl obj, DependencyProperty value) {
obj.SetValue(TimeAgoProperty, value);
}
public static readonly DependencyProperty TimeAgoProperty = DependencyProperty.RegisterAttached("TimeAgo",
typeof(DateTime), typeof(TimeAgoBehaviour),
new UIPropertyMetadata(DateTime.UtcNow, OnTimeAgoChanged));
private static void OnTimeAgoChanged(object sender, DependencyPropertyChangedEventArgs e) {
var newDate = (DateTime)e.NewValue;
EventHandler oldEvent;
if (Events.TryRemove(sender.GetHashCode(), out oldEvent)) {
LazyTimer.Value.Tick -= oldEvent;
}
if (DateTime.MinValue == newDate) {
return;
}
var doUpdate = new EventHandler((s, args) => {
ContentControl control = sender as ContentControl;
control.Content = TimeAgoConverter.GetTimeAgo(newDate);
});
doUpdate(sender, new EventArgs());
Events.TryAdd(sender.GetHashCode(), doUpdate);
LazyTimer.Value.Tick += doUpdate;
}
}
Xaml:
<Label local:TimeAgoBehaviour.TimeAgo="{Binding InitialisedDate}" />
Below is the original response:
You could look to do it through an AttachedBehaviour
, which invalidates the binding every minute, causing it to be re-evaluated, the below works, as a starting point extend where you need to:
Code:
public static class PropertyToInvalidateBehaviour {
static Lazy<DispatcherTimer> LazyTimer = new Lazy<DispatcherTimer>(() => {
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
timer.Start();
return timer;
});
static ConcurrentDictionary<int, EventHandler> Events = new ConcurrentDictionary<int, EventHandler>();
public static DependencyProperty GetPropertyToInvalidate(DependencyObject obj) {
return (DependencyProperty)obj.GetValue(PropertyToInvalidateProperty);
}
public static void SetPropertyToInvalidate(DependencyObject obj, DependencyProperty value) {
obj.SetValue(PropertyToInvalidateProperty, value);
}
public static readonly DependencyProperty PropertyToInvalidateProperty = DependencyProperty.RegisterAttached("PropertyToInvalidate",
typeof(DependencyProperty), typeof(PropertyToInvalidateBehaviour),
new UIPropertyMetadata(null, OnPropertyToInvalidateChanged));
private static void OnPropertyToInvalidateChanged(object sender, DependencyPropertyChangedEventArgs e) {
var propertyToInvalidate = e.NewValue as DependencyProperty;
EventHandler oldEvent;
if (Events.TryRemove(e.Property.GetHashCode(), out oldEvent)) {
LazyTimer.Value.Tick -= oldEvent;
}
if (null == propertyToInvalidate) {
return;
}
var doUpdate = new EventHandler((s, args) => {
BindingExpression binding = BindingOperations.GetBindingExpression(sender as DependencyObject, propertyToInvalidate);
binding.UpdateTarget();
});
Events.TryAdd(e.Property.GetHashCode(), doUpdate);
LazyTimer.Value.Tick += doUpdate;
}
}
Xaml:
<Label Content="{Binding Path=InitialisedDate, Converter={StaticResource cnvTimeAgo}}" local:PropertyToInvalidateBehaviour.PropertyToInvalidate="Label.Content" />`
Upvotes: 1
Reputation: 10384
Big smart ViewModels, dumb Views, and any model, the best MVVM approach? contains the code for WPF app where ViewModel (and View bound to it) is updated by timer. It is there by every 1 sec (1000 ms)
Upvotes: 1
Reputation: 4434
Look into the dispatch timer and perform a record update every minute
That's what I would do
Upvotes: 1