0

Let's say here is a piece of data:

public string DisplayTimeLeft {
    get { return (SessionEnd - DateTime.Now).ToString(); }
}

I think I can't call something like OnPropertyChanged(nameof(DisplayTimeLeft)); every second because there are a lot of items like this one, so it'd be quite a strange solution.

For a single TextBlock (on some SelectedItemPage) I call something like this every second:

TimeLeftTextBlock.GetBindingExpression(TextBlock.TextProperty)?.UpdateTarget();

But what if I want to show an actual time left in ItemsSource (or ListBox)? What's the best way to do it?

Surfin Bird
  • 488
  • 7
  • 16
  • Duplicate? http://stackoverflow.com/questions/6123998/binding-to-time-dependent-properties – Vadim Martynov Mar 24 '16 at 13:00
  • @VadimMartynov “Set up a timer that Just calls PropertyChanged,” — even for, like, thousand items? Even if most of them aren't visible at the moment (but already have some PropertyChanged listeners)? Isn't it too much? – Surfin Bird Mar 24 '16 at 13:10
  • 1
    Well, I hope you'll find other solution, but in my opinion there is no better way to do it. `GetBindingExpression` and similar solutions will work slower. – Vadim Martynov Mar 24 '16 at 13:19
  • 2
    It sounds like you don't even know yet if this problem exists. First try the obvious, simple solution that's very easy to implement. *Then*, if you have problems, look at fixing them. If you've got a thousand items, only a few of them visible, AND there's a noticeable lag issue in practice, you might look at giving the local viewmodels each a DisplayTimeLeft of its own which returns the value of the main VM's DisplayTimeLeft. Each child VM listens to the main VM for DisplayTimeLeft changes. But they only fire their own PropertyChanged(nameof(DisplayTimeLeft)) if they happen to be visible. – 15ee8f99-57ff-4f92-890c-b56153 Mar 24 '16 at 14:28
  • @EdPlunkett Thanks, I'll try it. But I was thinking if there is any other way (and I think I found it), after all, number of elements could be ten thousand or more; and they already have some PropertyChanged listener (for example, for filtering). – Surfin Bird Mar 24 '16 at 16:00
  • Another very important point I forgot to mention: Bear in mind that with virtualization, the controls that aren't visible *may not exist at all* (and hence aren't responding to any notifications). For one example, In a `TabControl` populated by its `ItemsSource`, the only `TabItem` whose contents actually exist is the currently selected one. I can't overstress the importance of *not optimizing prematurely*. – 15ee8f99-57ff-4f92-890c-b56153 Mar 24 '16 at 16:12

0 Answers0