0

I have a simple object Action that has a property Code. Depending on its Code, I want to select different DataTemplates a it is also possible for the user to change the Code through a ComboBox.

public class Action : INotifyPropertyChanged
{
    public Action()
    {
        Parameters = new List<Parameter>();
    }

    public int ActionID { get; set; }

    public int StepID { get; set; }

    public int Code { get; set; }

    [NotMapped]
    public List<Parameter> Parameters { get; set; }
}

So I was looking at this answer: https://stackoverflow.com/a/18000310/2877820

I tried the implement the solution like this:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
    var action = (ASI.RecipeManagement.Data.Action) item;
    if (action == null) return null;

    PropertyChangedEventHandler lambda = null;
    lambda = (o, args) =>
    {
        if (args.PropertyName == "Code")
        {
            action.PropertyChanged -= lambda;
            var cp = (ContentPresenter)container;
            cp.ContentTemplateSelector = null;
            cp.ContentTemplateSelector = this;
        }
    };
    action.PropertyChanged += lambda;

    if (action.Code == 0)
        return NoParamTemplate;

    if (action.Code == 1)
        return OneParamTemplate;

    if (action.Code == 2)
    {
        if (action.Parameters[0].Type == ParameterInputTypes.List)
        {
            return ComboBoxParamTemplate;
        }
        return TwoParamTemplate;
    }
    return null;
}

Sadly it does not seem to work for me. Can anybody help me out? What am I doing wrong right here?

user2877820
  • 287
  • 4
  • 19
  • Step through it with debugger and see what's going on. Where is it returning, what are the values of everything when it does? – Sean O'Neil Nov 20 '17 at 11:47
  • @SeanO'Neil PropertyChanged never gets called. I change the properties correctly but it seems like I dont get notified – user2877820 Nov 20 '17 at 11:55

1 Answers1

1

A DataTemplateSelector does't respond to property change notifications. As a workaround, you could use a ContentControl with DataTriggers in the ItemTemplate, .e.g.:

<ComboBox ...>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}">
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="ContentTemplate" Value="{StaticResource NoParamTemplate}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Code}" Value="1">
                                <Setter Property="ContentTemplate" Value="{StaticResource OneParamTemplate}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Code}" Value="2">
                                <Setter Property="ContentTemplate" Value="{StaticResource TwoParamTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
mm8
  • 163,881
  • 10
  • 57
  • 88