I'm able to declare an integer or double value in xaml. However, I can't add a decimal value. It builds ok, but then I get:
System.Windows.Markup.XamlParseException: The type 'Decimal' was not found.
Here's 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>
Here's how I'm declaring the system namespace:
xmlns:system="clr-namespace:System;assembly=mscorlib"
Edit: Workaround: As Steven mentioned, adding the resource through code-behind seems to work fine:
Resources.Add("ADecimalValue", new Decimal(1000.0));
Edit: Answer: Doing exactly the same thing in WPF seems to work fine. So I guess this is a hidden silverlight restriction. Thanks to Steven for this finding.
I have confirmed your finding that the Decimal type does not appear to work as a static resource in a UserControl's resources section. I do however see a couple workarounds that have been discussed here on StackOverflow, and that i have just personally verified to work with the Decimal type in Silverlight: Access codebehind variable in XAML
The workarounds include:
The second workaround can be done like this:
<sdk:Label Name="label1" Content="{Binding ElementName=root, Path=DecimalProperty}" />
...where the root usercontrol tag is defined like this (this idea is from the link above also):
<UserControl x:Class="SilverlightDecimal.MainPage" x:Name="root" .... >
and this is in your user control's code-behind:
public decimal DecimalProperty
{
get
{
...
}
set
{
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With