[英]Set WPF Binding.StringFormat Property on TextBox via Style
I have an WPF application contains many TextBox
es having different kind of Bindings which all share the same StringFormat
property (its a technical application, the Textboxes should display values with units "xxx mm"...) 我有一个WPF应用程序包含许多具有不同类型的Bindings的TextBox
,它们共享相同的StringFormat
属性(它是一个技术应用程序,Textboxes应显示单位为“xxx mm”的值...)
I want to set up the Binding in the XAML/Designer, but I'd like to avoid setting the TextFormat property on every individual Binding. 我想在XAML / Designer中设置Binding,但我想避免在每个Binding上设置TextFormat属性。 Is there a way to do this using Styles? 有没有办法使用样式?
If I try to set the Binding in a Setter
for the Text property like 如果我尝试在Setter
为Text属性设置Binding,比如
<Style x:Name="mmtext" TargetType="TextBox" x:Key="mmtext">
<Setter Property="Text" Value="{Binding Path=A,StringFormat={}{0} mm}" />
</Style>
I need to provide a Path
in the Setters Value property, and I cannot define any binding in the XAML itself (as this would override the value set in the Style). 我需要在Setters Value属性中提供一个Path
,并且我无法在XAML本身中定义任何绑定(因为这会覆盖Style中设置的值)。
Is there a way to set/modify only the StringFormat
property in a single Binding (ie the Binding for the Text
property) using a Style? 有没有办法使用Style在单个Binding(即Text
属性的Binding)中设置/修改StringFormat
属性?
Or do I need to look for templating or a custom control? 或者我是否需要寻找模板或自定义控件?
you could probably bind the DataContext of the textbox rather than the text property 你可以绑定文本框的DataContext而不是text属性
<TextBox DataContext="{Binding Path=A}" />
and then use a setter like 然后使用像
<Style x:Name="mmtext" TargetType="TextBox" x:Key="mmtext">
<Setter Property="Text" Value="{Binding Path=., StringFormat={}{0} mm}" />
</Style>
for a TwoWay binding you will need a converter anyways to get rid of the extra mms 对于TwoWay绑定,无论如何都需要一个转换器来摆脱额外的mms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.