深入探索WinRT组件、模板与自定义元素
1. 附加属性在进度条模板中的应用
在进度条模板里,TextBlock可使用附加属性,示例如下:
<TextBlock HorizontalAlignment="Center" Foreground="White" VerticalAlignment="Center" Visibility="{Binding (local:ProgressBarProperties.ShowText), RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource bool2vis}}"> <Run Text="{Binding Value, RelativeSource= {RelativeSource TemplatedParent}}" /> <Span>%</Span> </TextBlock>属性路径周围的括号是必需的,不然XAML解析器无法正确理解表达式,从而导致运行时绑定失败。这里使用的转换器用于将布尔值转换为Visibility枚举。
定义和注册附加属性虽简单,但较为繁琐。可通过定义宏来自动处理这些样板代码。例如,在ProgressBarProperties类中定义附加属性:
DECL