Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've used the following method to force re-application of a DataTemplateSelector.</p> <p>Derive from ObservableCollection and add a method that raises NotifyCollectionChangedEventArgs with NotifyCollectionChangedAction.Reset.</p> <pre><code>public class MyThingCollection : ObservableCollection&lt;MyThing&gt; { public void RaiseResetCollection() { OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } </code></pre> <p>Your view model exposes an instance of this type and your ItemsControl binds to that.</p> <pre><code>public class MyViewModel : ... (view model base) { public MyThingCollection Items{get; private set;} } &lt;ItemsControl ItemsSource="{Binding Items}" ItemsTemplateSelector="{StaticResource MyTemplateSelector}" ... </code></pre> <p>When you need your DataTemplateSelector to be re-applied call RaiseResetCollection on the collection.</p> <p>I generally use DataTemplateSelector like this</p> <pre><code>public class MyTemplateSelector : DataTemplateSelector { public DataTemplate Template1 { get; set; } public DataTemplate Template2 { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { ... return Template1 or Template2 depending on item } ... } &lt;DataTemplate x:Key="MyTemplate1" DataType="{x:Type MyType1}"&gt; ... &lt;/DateTemplate&gt; &lt;DataTemplate x:Key="MyTemplate2" DataType="{x:Type MyType2}"&gt; ... &lt;/DateTemplate&gt; &lt;local:MyTemplateSelector x:Key="MyTemplateSelector" Template1="{StaticResource MyTemplate1}" Template2="{StaticResource MyTemplate2}" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload