I have a TextBox style that formats a number if the box is unfocused, but leaves the number unformatted whlie it’s being edited.
This is the style I want for multiple number TextBoxes, but they all contain different Text bindings. The only difference between the regular Text setter and the Triggered Text setter is that the Triggered one has StringFormat=N2 in the binding.
Is there a way I can make this style generic, such as only changing the StringFormat property of the binding in the DataTrigger?
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Text" Value="{Binding SomeValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The only option I see there would be to create an attached property for the StringFormat and use a multiBinding.
Not quite what you wanted, but close enough, I guess…
you have more info on this (kind of duplicate) question on S.O.:
Modifying the Parameters of a TextBox's Text Binding through the use of a Style