How to trigger DataTemplateSelector when property changes?
Asked Answered
R

4

32

I have ContentPresenter with DataTemplateSelector:

    ...

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var model = item as ItemControlViewModel;

        if (model.CurrentStatus == PrerequisitesStatus.Required)
        {
            return RequiredTemplate;
        }

        if (model.CurrentStatus == PrerequisitesStatus.Completed)
        {
            return FinishedTemplate;
        }

        ...

        return InProgressTemplate;
    }

When CurrentStatus is changed, OnPropertyChanged is called.

I need somehow to trigger this DataTemplateSelector when the property is changed and change ContentPresenter DataTemplate. Any suggestions?

Threre are similar questions: 1 2, but I don't want to use any DataTriggers, because of too much states.

Tried to play with DataTriggers

    <ContentPresenter
        Grid.Column="1"
        Height="16"
        Width="16"
        Margin="3">
        <ContentPresenter.Triggers>
            <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="0">
                <Setter Property="ContentPresenter.ContentTemplate" Value="{StaticResource ResourceKey=_requiredStatusTemplate}" />
            </DataTrigger>
        </ContentPresenter.Triggers>
    </ContentPresenter>

But got an error: Triggers collection members must be of type EventTrigger :(

Robins answered 3/1, 2012 at 16:28 Comment(4)
@apt0r Have you considered using VisualStateManager as opposed to Templates?Gauldin
No, I think it is not suited here. I need to change template, not a property.Robins
Had the same issue myself in the past and switched to DataTriggers in order to get it work, I don't think there's a better solution to this...Leitmotif
@Leitmotif Could you post some example?Robins
L
41

As you requested an example with datatriggers in the comments, here you are:

A FrameworkElement can only have EventTriggers, therefore you get the error Message Triggers collection members must be of type EventTrigger

And also don't use a ContentPresenter directly, it is meant to be used inside a ControlTemplate. Better use a ContentControl when you want to have dynamic content. See What's the difference between ContentControl and ContentPresenter?

And finally here's a suggestion to your DataTrigger issue. I have put it inside a style for reusability ....

XAML :

<Window x:Class="WpfApplication88.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>

    <DataTemplate x:Key="requiredTemplate">
      <TextBlock Text="requiredTemplate"></TextBlock>
      <!--your stuff here-->
    </DataTemplate>

    <DataTemplate x:Key="completedTemplate">
      <TextBlock Text="CompletedTemplate"></TextBlock>
      <!--your stuff here-->
    </DataTemplate>

    <Style x:Key="selectableContentStyle" TargetType="{x:Type ContentControl}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="Required">
          <Setter Property="ContentTemplate" Value="{StaticResource requiredTemplate}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=CurrentStatus}" Value="Completed">
          <Setter Property="ContentTemplate" Value="{StaticResource completedTemplate}" />
        </DataTrigger>
        <!--  your other Status' here -->
      </Style.Triggers>
    </Style>

  </Window.Resources>

  <Grid>
    <ContentControl Width="100" Height="100" Style="{StaticResource selectableContentStyle}"/>
  </Grid>

</Window>
Leitmotif answered 4/1, 2012 at 16:0 Comment(2)
Note that when using DataTriggers the xaml engine always creates all possible datatemplates even if they are not in use which can lead to some performance issues when using complex gui.Agateware
As per linked question: ContentPresenter is designed to be used as-is while ContentControl is designed to be extended (inherited from). Recommending ContentControl in this particular scenario is bad coding advice.Tulley
I
6

I could be wrong, but I believe the DataTemplateSelector is only used when the ItemContainerGenerator creates a container for an item added to the collection. Because a new container isn't generated when a property value changes, a new DataTemplate is never going to be applied via the selector.

As suggested in the comments, I would recommend you look at the VisualStateManager or data triggers, otherwise you're going to have to recreate the container for every item when one or more properties change value.

Insight answered 3/1, 2012 at 18:50 Comment(0)
G
2

Just as an extra choice - if you want to stick to your templates, just use s binding with converter.

Gauldin answered 3/1, 2012 at 20:15 Comment(0)
T
2

I came up with a behavior that would theoretically do this.

C#:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

public class UpdateTemplateBehavior : Behavior<ContentPresenter>
{
    public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(UpdateTemplateBehavior), new FrameworkPropertyMetadata(null, OnContentChanged));
    public object Content
    {
        get => GetValue(ContentProperty);
        set => SetValue(ContentProperty, value);
    }
    static void OnContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is UpdateTemplateBehavior behavior)
            behavior.Update();
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), typeof(object), typeof(UpdateTemplateBehavior), new FrameworkPropertyMetadata(null, OnValueChanged));
    public object Value
    {
        get => GetValue(ValueProperty);
        set => SetValue(ValueProperty, value);
    }
    static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        if (sender is UpdateTemplateBehavior behavior)
            behavior.Update();
    }

    public UpdateTemplateBehavior() : base() { }

    protected override void OnAttached()
    {
        base.OnAttached(); 
        Update();
    }

    void Update()
    {
        if (Content != null)
        {
            BindingOperations.ClearBinding(AssociatedObject, ContentPresenter.ContentProperty);
            AssociatedObject.Content = null;

            BindingOperations.SetBinding(AssociatedObject, ContentPresenter.ContentProperty, new Binding() { Path = nameof(Content), Source = this });
        }
    }
}

XAML:

<ContentPresenter ContentTemplateSelector="{StaticResource MySelector}">
    <i:Interaction.Behaviors>
        <Behavior:UpdateTemplateBehavior Content="{Binding SomeContent}"
            Value="{Binding SomeValue}"/>
    </i:Interaction.Behaviors>
</ContentPresenter>

The content is "updated" (by clearing and then resetting the binding) when the content (in this example, "SomeContent") and an arbitrary value (in this example, "SomeValue") is changed, as well as when the behavior is first attached.

An update is not made unless the content is not null (my project-specific requirement). Not updating upon attaching may avoid unintentionally updating twice at once, but if the value is initially null, an update wouldn't occur until the value changes at least once.

Note: In the above example, I am not sure if the behavior has the same data context as the ContentPresenter. I use a helper class that I did not include here for brevity. Keep that in mind when testing...

Tulley answered 14/1, 2022 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.