DeM*_*ama 4 wpf binding static properties
在 WPF 中,可以绑定到静态属性。现在我知道两种方法:
Content="{x:Static stat:Statics.CurrentUser}"
Run Code Online (Sandbox Code Playgroud)
或者:
Content="{Binding Source={x:Static stat:Statics.CurrentUser}}"
Run Code Online (Sandbox Code Playgroud)
这两种方法有什么区别吗?
这种情况下的主要区别是x:Static不执行额外的转换
当您创建不直接是属性值类型的 x:Static 引用时要小心。在 XAML 处理序列中,来自标记扩展的提供值不会调用附加值转换。即使您的 x:Static 引用创建了一个文本字符串,并且基于文本字符串的属性值的值转换通常发生在该特定成员或返回类型的任何成员值时也是如此。
所以让我们说你做
<TextBlock Text="{x:Static SystemColors.ActiveBorderBrush}"/>
Run Code Online (Sandbox Code Playgroud)
这将导致运行时错误:
'#FFB4B4B4' 不是属性 'Text' 的有效值。
因为SolidColorBrush不是String同时
<TextBlock Text="{Binding Source={x:Static SystemColors.ActiveBorderBrush}}"/>
Run Code Online (Sandbox Code Playgroud)
将正常工作并显示#FFB4B4B4,因为它将执行ToString()转换。同样,如果Binding您无法访问静态对象的实例属性,例如您将无法获得Color该画笔的属性
<TextBlock Text="{Binding Source={x:Static SystemColors.ActiveBorderBrush}, Path=Color}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1845 次 |
| 最近记录: |