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?
4 Answers
Please try this.
Add namespace xmlns:sys="clr-namespace:System;assembly=mscorlib" in xaml
<sys:Int16 x:Key="IntNo">1</sys:Int16> or
<sys:Int32 x:Key="IntNo1" >1</sys:Int32>
Note : Similarly You can use for Double value also.

- 8,450
- 1
- 22
- 40
-
For current .NET-runtimes the namespace changed to xmlns:sys="clr-namespace:System;assembly=System.Runtime". – MarkusParker Apr 01 '22 at 09:59
If not interested in declaring it as resource, you can declare it in-line somewhat like this:
<Button>
<Button.Tag>
<sys:Int32>5</sys:Int32>
</Button.Tag>
</Button>

- 79,502
- 12
- 161
- 185
-
1Unfortunately it is too many letters too. I need set Tag for several elements like `Tag="int:0", Tag="int:1"` and so on – arteny Jan 04 '14 at 14:54
-
`Tag` property is of type `object`. Even when you want to access it from code behind, you have to do typecast `(int)button.Tag`. Can you tell the use case? – Rohit Vats Jan 04 '14 at 14:57
-
1yes, but (int)button.Tag is more simple than int.Parse((string)button.Tag) :) ok, as I understand to use string values for this looks is simplest existing way – arteny Jan 04 '14 at 15:15
-
Yeah there is no other way in XAML. Or using Resource also seems good to me. – Rohit Vats Jan 04 '14 at 15:22
-
@Arteny - For either approach, you can make a static extension method that gets the tag property and casts it to int. E.g. `public static int TagAsInt(this YourBaseElementClass e) { return (int)e.Tag; }`. Then usage is trivial `myElement.TagAsInt()`. If it is easier to define Tag as a string, you just change that method to do the parse, and use it exactly the same. – ToolmakerSteve Jun 18 '18 at 19:40
-
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>

- 659
- 1
- 5
- 15
In XAML 2009 you can simply use the "x"
prefix, like x:Boolean
, x:Decimal
or x:Int32
.
See Microsoft - Built-in types for common XAML language primitives
Example:
This example is from a WinUI 3 application (WinUI 3 XAML is very similar to UWP XAML and mostly similar to WPF XAML)
<Window
x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button x:Name="MyButton" Click="MyButton_Click" Content="Print 'Tag' value to console">
<Button.Tag>
<x:Int32>42</x:Int32>
</Button.Tag>
</Button>
</Grid>
</Window>
Code behind:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
int value = (int) MyButton.Tag;
Debug.WriteLine(value);
}
You can also spefify a command parameter that accepts an int
in that way:
<Button Command="{x:Bind ViewModel.AddMinutesCommand}" Content="+ 30 Minutes">
<Button.CommandParameter>
<x:Int32>30</x:Int32>
</Button.CommandParameter>
</Button>
There seems to be a confusion about the availability of XAML 2009 in various technologies: Stackoverflow - Can XAML 2009-related markup extensions be used in WPF?
Honestly, I also do not understand why my working example code can just use xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
for the x
namespace
instead of having to specify something like 2009/xaml
here.
Feel free to change this answer, if you can clarify this.

- 5,165
- 1
- 37
- 50
-
This one is way more up to date than the other answers. Works on UWP and WinUI3, thanks for sharing! – AtosNicoS Sep 21 '22 at 12:52