Determining value of ContentTempateSelector based

2019-08-13 00:07发布

问题:

I am unable to change the contents of the ContentTemplateSelector. Based on the two properties Eligibility and ListOfThings, the text block is updated and it is achieved by multibinding. However, the multi binding approach could not be applied for the template selector. Here is my code :

XAML :

<Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <ContentControl
         Grid.Column="0"
         Style="{StaticResource StatusIconStyle}"
         Content="{Binding}"
         ContentTemplateSelector="{StaticResource IconTemplateSelector}" />
     <TextBlock
         Grid.Column="1"
         TextWrapping="Wrap">
         <TextBlock.Text>
             <MultiBinding Converter="{StaticResource ListOfThingsToEligibilityString}">
                   <Binding Path="Eligibility"/>
                   <Binding Path="ListOfThings" />
             </MultiBinding>
         </TextBlock.Text>
      </TextBlock>
</Grid>

TemplateSelector :

public override DataTemplate IconTempateSelector(object item,...)
    {
        var obj = (CompleteObjectInfo)item;
        bool isEligible = obj.Eligibility.IsEligibile;
        bool hasIssues = obj.ListOfThings != null && obj.ListOfThings.Any();
        List<IThings> things = obj.ListOfThings as List<IThings>;
        //some logic
        if (condition 1)
        {
          return (DataTemplate)App.Current.TryFindResource("SuccessVImage");
        }
        else if (condition 2)
        {
            return (DataTemplate)App.Current.TryFindResource("WarningVImage");
        }
        else if (consition 3)
        {
           return (DataTemplate)App.Current.TryFindResource("ErrorVImage");
        }
        return null;
    }

MultiConverter:

ListOfThingsToEligibilityString :
public object Convert(object[] values,...)
    {
        if (null conditions satisfied)
        {
            EligibilityDetails eligibility = values[0] as EligibilityDetails ;
            List<IThings> things= values[1] as List<IThings>;
            bool isEligible = eligibility.IsEligibile;
            bool hasIssues = things.Any();
            // some code
            if (condition 1)
            {
                return "Message 1";
            }
            else if (condition 2)
            {
                return "Message 2";
            }
            else if (condition 3)
            {
                return "Message3";
            }
            else if (condition 4)
            {
                return "Message 4"
            }
        }
        return string.Empty;
    }

Any help would be appreciated.

回答1:

A ContentTemplateSelector does not trigger when a ProperyChanged event is raised. It's only used to select the initial ContentTemplate.

You could try to replace the ContentTemplateSelector with a Style that contains DataTriggers:

<ContentControl Grid.Column="0" Content="{Binding}">
    <ContentControl.Style>
        <Style TargetType="ContentControl" BasedOn="{StaticResource StatusIconStyle}">
            <Style.Triggers>
                <DataTrigger Value="Message 1">
                    <DataTrigger.Binding>
                        <MultiBinding Converter="{StaticResource ListOfThingsToEligibilityString}">
                            <Binding Path="Eligibility"/>
                            <Binding Path="ListOfThings" />
                        </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter Property="ContentTemplate" Value="{StaticResource SuccessVImage}" />
                </DataTrigger>
                <!-- and so on...-->
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>