Determining value of ContentTempateSelector based on two properties

爷,独闯天下 提交于 2019-12-24 14:05:59

问题


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>


来源:https://stackoverflow.com/questions/49932582/determining-value-of-contenttempateselector-based-on-two-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!