How to declare a decimal value in XAML?

I can declare an integer or double value in xaml. However, I cannot add a decimal value. It works fine, but then I get:

System.Windows.Markup.XamlParseException: The type "Decimal" was not found.

Here is the xaml code:

<UserControl.Resources> <system:Int32 x:Key="AnIntValue">1000</system:Int32><!--Works!--> <system:Double x:Key="ADoubleValue">1000.0</system:Double><!--Works!--> <system:Decimal x:Key="ADecimalValue">1000.0</system:Decimal><!--Fails at runtime--> </UserControl.Resources> 

This is how I declare a system namespace:

 xmlns:system="clr-namespace:System;assembly=mscorlib" 

Edit: Workaround: As Stephen noted, adding a resource using code-code seems to work fine:

 Resources.Add("ADecimalValue", new Decimal(1000.0)); 

Edit: answer: Doing exactly the same thing in WPF seems to work fine. Therefore, I assume that this is a hidden limitation of Silverlight. Thanks Steven for this discovery.

+6
source share
1 answer

I confirmed your discovery that the Decimal type does not work as a static resource in the UserControl resource section. However, I can see a couple of workarounds discussed here at StackOverflow, and that I personally personally tested working with the Decimal type in Silverlight: Accessing the codebehind variable in XAML

Workarounds:

  • adding a resource from the code (see the link above)
  • Property reference in code using a binding of the "elementname" type
  • Access to the Decimal public property for the user controls the data context property.

The second workaround can be done as follows:

 <sdk:Label Name="label1" Content="{Binding ElementName=root, Path=DecimalProperty}" /> 

... where the usercontrol root tag is defined like this (this idea also applies to the link above):

 <UserControl x:Class="SilverlightDecimal.MainPage" x:Name="root" .... > 

and this is in your user control code:

 public decimal DecimalProperty { get { ... } set { ... } } 
+2
source

Source: https://habr.com/ru/post/895160/


All Articles