wpf 有没有一种简单的方法可以在 xaml 中指定 int 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20920645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a simple way to specify int value in xaml
提问by arteny
I want set Tag property with int value in xaml. But defining int in resources and then reference this resource as binding looks not a perfect way for me. It is easier just to convert string value to int from code. So, is there some way to easy set int value in xaml?
我想在 xaml 中使用 int 值设置 Tag 属性。但是在资源中定义 int 然后将此资源作为绑定引用对我来说并不是一种完美的方式。将字符串值从代码转换为 int 更容易。那么,有没有办法在 xaml 中轻松设置 int 值?
采纳答案by Rohit Vats
If not interested in declaring it as resource, you can declare it in-linesomewhat like this:
如果对将其声明为资源不感兴趣,您可以像这样内联声明它:
<Button>
<Button.Tag>
<sys:Int32>5</sys:Int32>
</Button.Tag>
</Button>
回答by Heena Patil
Please try this.
请试试这个。
Add namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"in xaml
在 xaml 中 添加命名空间 xmlns:sys="clr-namespace:System;assembly=mscorlib"
<sys:Int16 x:Key="IntNo">1</sys:Int16> or
<sys:Int32 x:Key="IntNo1" >1</sys:Int32>
Note : Similarly You can use for Double valuealso.
注意:同样,您也可以使用Double 值。
回答by Code Ars
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Grid>
<Grid.Resources>
<sys:Int32 x:Key="IntValue" >1</sys:Int32>
</Grid.Resources>
<Button x:Name="Button" Tag="{StaticResource IntValue}"></Button>
</Grid>
Is it simple enough? The above sample will be suitable if you going to use your Value in several places. Otherwise:
够简单吗?如果您打算在多个地方使用您的价值,则上述示例将是合适的。除此以外:
<Button x:Name="Button" >
<Button.Tag>
<sys:Int32>1</sys:Int32>
</Button.Tag>
</Button>

