2

这适用于 Windows Phone 8。

假设我有一组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}" />

为什么这种方式行不通?重用这种风格会很好(实际上是一组)。

4

0 回答 0