Changing ItemTemplate at Runtime - Triggers? VSM? Something else? RRS feed

  • Question

  • First post, first-time .NET developer trying to slog my way through a project. Hoping someone can help me out!

    I have a ListBox that displays a collection of Clip objects called Contents:

    <ListBox ItemsSource="{Binding Contents}">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Status}" />
                    <ProgressBar Value="{Binding DownloadProgress}" />

    The Status property in the Clip object is an enum with possible values of Remote, Local, or Downloading. These values implement INotifyPropertyChanged and will change during runtime. I need to be able to make changes dynamically to the template based on the value of Status - for example, making the ProgressBar visible only when Status is "Downloading".

    In trying desperately to search for an answer, it seems that DataTriggers used to be a way of doing it - now it sounds like VisualStateManager has taken over that functionality. But neither seems exactly like they were made for this task (i.e. I don't really need to animate anything... is VSM still applicable?).

    Am I at least headed in the right direction?

    Thanks so much!!

    • Edited by Andrew Ewert Thursday, February 13, 2014 5:09 AM formatting
    Thursday, February 13, 2014 5:08 AM

All replies

  • You can use either Visual States or a DataTemplateSelector. For your ProgressBar case you could also bind its visibility. Which to choose depends on how drastic the changes you want to make are and where exactly you want to change them.

    Visual states aren't inherently tied to animations, but they make animating changes easy. For example, you may want to fade in your ProgressBar rather than having it just appear.


    Thursday, February 13, 2014 6:59 AM
  • Thanks Rob!

    I did start out with a DataTemplateSelector and that works great upon initial load, but doesn't seem to get re-evaluated when a bound property changes. Is there some way to have the SelectTemplateCore get called whenever a property changes?

    Friday, February 14, 2014 2:38 AM