简体   繁体   English

WPF - 可以将StringFormat绑定到属性吗?

[英]WPF - Can StringFormat be bound to a property?

Given a WPF text box, as an example: 给定一个WPF文本框,作为示例:

<TextBox Text="{Binding Path=blahProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:yyyy.MM.dd HH:mm:ss\}}"/>

Could the StringFormat I've got there instead be bound to an underlying property string? StringFormat我可以在那里绑定到底层属性字符串吗? - ie could the "yyyy.MM.dd HH:mm:ss" be provided by a property on the View (or ViewModel) instead of being hard-coded in the xaml? - 即“yyyy.MM.dd HH:mm:ss”是否可以由View(或ViewModel)上的属性提供,而不是在xaml中进行硬编码?

No, a {Binding} cannot be set on the StringFormat property of the Binding because StringFormat is not a dependency property. 不,无法在BindingStringFormat属性上设置{Binding} ,因为StringFormat不是依赖项属性。

You can only bind to a DependencyProperty of a DependencyObject , ie the target property of a binding must be a dependency property. 您只能绑定到DependencyPropertyDependencyObject ,即绑定的target属性必须是依赖属性。

You may set it to the the value of static property though: 您可以将其设置为静态属性的值,但:

StringFormat={x:Static local:MainWindow.StaticProperty}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM