Xaml 相对资源自绑定在控件上工作,但不在样式设置器内
这是针对Windows Phone 8的 假设我有一组Xaml 相对资源自绑定在控件上工作,但不在样式设置器内,xaml,windows-phone-7,windows-phone-8,windows-phone,Xaml,Windows Phone 7,Windows Phone 8,Windows Phone,这是针对Windows Phone 8的 假设我有一组TextBlocks,我想根据它们绑定到的文本更改它们的前景色 if (text == "0") return Red; else return Green; 我可以使用相对源绑定来实现这一点: <TextBlock Foreground="{Binding Text, Converter={StaticResource TextBrushConverter}, RelativeSource={RelativeSource S
TextBlocks
,我想根据它们绑定到的文本更改它们的前景色
if (text == "0")
return Red;
else
return Green;
我可以使用相对源绑定来实现这一点:
<TextBlock Foreground="{Binding Text, Converter={StaticResource TextBrushConverter}, RelativeSource={RelativeSource Self}}" Text="{Binding SomeText}" />
但如果我将其移动到样式设置器,它将不再像预期的那样工作:
<Style x:Name="MyStyle" TargetType="TextBlock">
<Style.Setters>
<Setter Property="Foreground"
Value="{Binding Text,
Converter={StaticResource TextBrushConverter},
RelativeSource={RelativeSource Self}}" />
</Style.Setters>
</Style>
<TextBlock Style="{StaticResource MyStyle}" Text="{Binding SomeText}" />
为什么这样不行?重新使用这种样式(实际上是一套样式)会很好。您是否尝试了
TemplatedParent
而不是Self
?