整数类型的WPF资源 [英] WPF resource of integer type
本文介绍了整数类型的WPF资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是WPF的新手,我想自己学习。我遇到了资源,我想问一个简单的(可能是一个愚蠢的)问题..我创建了这样的资源
< Window.Resources >
< sys:String x:键 = strHelloWorld > Hello,world!< / sys:String >
< sys:Int16 x:Key = intKey > 34 < / sys:Int16 >
< / Window.Resources >
我正在尝试使用这样的键值显示
< StackPanel 保证金 = 10 >
< TextBlock 文本 = {StaticResource strHelloWorld} FontSize = 56 / >
< TextBlock > 只是另一个 < TextBlock 文本 = {StaticResource strHelloWorld} / > 示例,但有资源!< / TextBlock >
< span class =code-keyword>< / Stack面板 >
我当时能够显示字符串类型属性,但如何在文本块中显示int资源或是否可能???
我能够做到这个使用标签...像这样
<前lang =xml> < 标签 内容 = {StaticResource intKey} > < /标签 >
提前谢谢
解决方案
自TextBlock以来,你不能直接这样做.Text
想要一个字符串,而不是Int16
。
标签
作品b因为Content
属性的类型是object
所以它是ContentPresenter
如果需要,将自动调用.ToString()
。
TextBlock中没有任何内容可以进行转换。
你可以使用绑定的StringFormat属性使其工作:
< TextBlock 文本 = {StaticResource strHelloWorld,StringFormat = {0}} FontSize = 56 < span class =code-attribute> / >
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
<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
<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
<Label Content="{StaticResource intKey}"></Label>
Thanks in advance
解决方案
You can't do it directly since theTextBlock.Text
wants a string, not anInt16
.
TheLabel
works because the type of theContent
property isobject
so it'sContentPresenter
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:
<TextBlock Text="{StaticResource strHelloWorld, StringFormat={0}}" FontSize="56" />
这篇关于整数类型的WPF资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文