Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am new to WPF and i trying to study by myself. I came across resource and i want to ask a simple (may be a dumb ) question.. I have created a resource like this

XML
<Window.Resources>
        <sys:String x:Key="strHelloWorld">Hello, world!</sys:String>
        <sys:Int16 x:Key="intKey">34</sys:Int16>
    </Window.Resources>



I am trying to display using the key value like this
XML
<StackPanel Margin="10">
     <TextBlock Text="{StaticResource strHelloWorld}" FontSize="56" />
     <TextBlock>Just another "<TextBlock Text="{StaticResource strHelloWorld}" />"  example, but with resources!</TextBlock>
 </StackPanel>


I was able to display the string type property without fail, but how can i display the int resource in a text block or is it possible ???

I was able to do this using label... like this

XML
<Label Content="{StaticResource intKey}"></Label>


Thanks in advance
Posted
Updated 13-Mar-14 20:18pm
v2

1 solution

You can't do it directly since the TextBlock.Text wants a string, not an Int16.
The Label works because the type of the Content property is object so it's ContentPresenter will automatically call .ToString() if necessary.
There's nothing in the TextBlock to do the conversion.
You can use the StringFormat property of the binding to make it work:
XML
<TextBlock Text="{StaticResource strHelloWorld, StringFormat={0}}" FontSize="56" />
 
Share this answer
 
Comments
Arjun Menon U.K 17-Mar-14 0:38am    
Thanks Matt

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900