根据两个属性确定 ContentTempateSelector 的值

Determining value of ContentTempateSelector based on two properties

我无法更改 ContentTemplateSelector 的内容。基于EligibilityListOfThings两个属性更新文本块,通过multibinding实现。但是,多重绑定方法不能应用于模板选择器。这是我的代码:

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>

模板选择器:

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;
    }

多转换器:

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;
    }

如有任何帮助,我们将不胜感激。

A ContentTemplateSelector 在引发 ProperyChanged 事件时 不会 触发。它只用于 select 初始 ContentTemplate.

您可以尝试用包含 DataTriggers:

Style 替换 ContentTemplateSelector
<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>