Reputation: 379
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?
Upvotes: 15
Views: 20808
Reputation: 352
You can create a custom MarkupExtension
to achieve the desired inline behavior.
[MarkupExtensionReturnType(typeof(int))]
public class IntExtension : MarkupExtension
{
public IntExtension(int value)
{
Value = value;
}
[ConstructorArgument("value")]
public int Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Value;
}
}
Then it's as simple as:
<Button Command="{Binding MyCommand}" CommandParameter="{local:Int 5}">
Upvotes: 0
Reputation: 5623
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.
Upvotes: 2
Reputation: 8654
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.
Upvotes: 23
Reputation: 81253
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>
Upvotes: 13
Reputation: 659
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>
Upvotes: 10