WPF - StringFormat 可以绑定到 属性 吗?
WPF - Can StringFormat be bound to a property?
以一个WPF文本框为例:
<TextBox Text="{Binding Path=blahProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:yyyy.MM.dd HH:mm:ss\}}"/>
我得到的 StringFormat 是否可以绑定到底层 属性 字符串? - 即 "yyyy.MM.dd HH:mm:ss" 是否可以由视图(或 ViewModel)上的 属性 提供,而不是在 xaml 中硬编码?
不,不能在 Binding
的 StringFormat
属性 上设置 {Binding}
因为 StringFormat
不是依赖项 属性 .
您只能绑定到 DependencyObject
的 DependencyProperty
,即绑定的目标 属性 必须是依赖项 属性。
不过您可以将其设置为静态值 属性:
StringFormat={x:Static local:MainWindow.StaticProperty}}
以一个WPF文本框为例:
<TextBox Text="{Binding Path=blahProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:yyyy.MM.dd HH:mm:ss\}}"/>
我得到的 StringFormat 是否可以绑定到底层 属性 字符串? - 即 "yyyy.MM.dd HH:mm:ss" 是否可以由视图(或 ViewModel)上的 属性 提供,而不是在 xaml 中硬编码?
不,不能在 Binding
的 StringFormat
属性 上设置 {Binding}
因为 StringFormat
不是依赖项 属性 .
您只能绑定到 DependencyObject
的 DependencyProperty
,即绑定的目标 属性 必须是依赖项 属性。
不过您可以将其设置为静态值 属性:
StringFormat={x:Static local:MainWindow.StaticProperty}}