Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to specify int value in xaml

Tags:

binding

wpf

xaml

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?

like image 342
arteny Avatar asked Jan 04 '14 11:01

arteny


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.

like image 107
Heena Avatar answered Oct 19 '22 05:10

Heena


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>
like image 21
Rohit Vats Avatar answered Oct 19 '22 04:10

Rohit Vats


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>
like image 26
Code Ars Avatar answered Oct 19 '22 06:10

Code Ars


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.

like image 27
Martin Avatar answered Oct 19 '22 04:10

Martin