基于两个属性确定 ContentTempateSelector 的值



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

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

任何帮助将不胜感激。

引发

ProperyChanged事件时不会触发ContentTemplateSelector。它仅用于选择初始ContentTemplate

您可以尝试将ContentTemplateSelector替换为包含DataTriggersStyle

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

相关内容

  • 没有找到相关文章

最新更新