I have defined a static resource:
<UserControl x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Width="255"
Height="300">
<UserControl.Resources>
<sys:Double x:Key="CornerRadiusValue">5</sys:Double>
</UserControl.Resources>
...
Later on in XAML file, I am trying to use that value when setting top left corner radius for a border:
<Border
Width="40"
Height="30"
BorderThickness="1,1,0,0"
BorderBrush="Red">
<Border.CornerRadius>
<CornerRadius TopLeft="{StaticResource CornerRadiusValue}" />
</Border.CornerRadius>
</Border>
In design time, everything works fine and changing the value for CornerRadiusValue
static resource changes the corner radius on a border. However, when I want to run this, I get an XamlParseException
exception with the message:
Cannot set read-only property `System.Windows.CornerRadius.TopLeft'.
What am I doing wrong? How do I make it work? Thanks.
MSDN:
You could try to bind the whole
CornerRadius
property and use a converter to get all the resources and create aCornerRadius
instance from them using the constructor.e.g. using only one value:
You could propbably make this more generic by searching for the resources going up the tree and not directly targeting the object on which the resources are defined.
(This is a Silverlight-only problem, in WPF your code works just fine, if you have a Silverlight question please avoid the WPF tag unless the problem actually exists in WPF)