simonalexander2005
simonalexander2005

Reputation: 4579

WPF - Can StringFormat be bound to a property?

Given a WPF text box, as an example:

<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? - i.e. 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?

Upvotes: 2

Views: 2360

Answers (1)

mm8
mm8

Reputation: 169160

No, a {Binding} cannot be set on the StringFormat property of the Binding because StringFormat is not a dependency property.

You can only bind to a DependencyProperty of a DependencyObject, i.e. the target property of a binding must be a dependency property.

You may set it to the the value of static property though:

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

Upvotes: 4

Related Questions