我无法更改ContentTemplateSelector
的内容。基于两个属性Eligibility
和ListOfThings
,文本块将更新,并通过多重绑定实现。但是,多绑定方法无法应用于模板选择器。这是我的代码:
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;
}
多转换:
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;
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
ContentTemplateSelector
在引发ProperyChanged
事件时触发 not 。它仅用于选择初始ContentTemplate
。
您可以尝试将ContentTemplateSelector
替换为包含Style
的{{1}}:
DataTriggers