Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the correct syntax for StringFormat in Binding tag?

What is the correct syntax for StringFormat in the second label?

<Label Text="{Binding StringFormat='{0:F2}', Source={x:Static sys:Math.PI}}"/>
<Label>
    <Label.Text>
        <Binding StringFormat="{0:F2}" Source="{x:Static sys:Math.PI}"/>
    </Label.Text>
</Label>

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is this a WPF application?
In WPF, Label does not have a Text property. There is a Content property, which is of type object. And therefore for this property no conversion to a string is used and, accordingly, the String Format is not applied.

For a TextBlock:

<TextBlock Text="{Binding StringFormat='{0:F2}', Source={x:Static sys:Math.PI}}"/> 

<TextBlock Text="{Binding StringFormat={}{0:F2}, Source={x:Static sys:Math.PI}}"/>

<TextBlock>
    <TextBlock.Text>
        <Binding StringFormat="{}{0:F2}" Source="{x:Static sys:Math.PI}"/>
    </TextBlock.Text>
</TextBlock>

<TextBlock>
    <TextBlock.Text>
        <Binding Source="{x:Static sys:Math.PI}">
            <Binding.StringFormat>
                <sys:String>{0:F2}</sys:String>
            </Binding.StringFormat>
        </Binding>
    </TextBlock.Text>
</TextBlock>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading