Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Dependency property - FrameworkElement
4 replies. Latest Post by Herthoren on November 7, 2009.
(0)
Herthoren
Member
85 points
82 Posts
11-06-2009 2:14 PM |
Hi!
Have a similar question, but that's fundamentally about a different issue.
While trying to solve it, realized that in Silverlight trying to set a Dependency Property from XAML to a FrameworkElement will always throw an exception.
I can set it even from Blend fully through the UI, it stil won't work.
In the same ControlTemplate:
<ContentControl Content="{StaticResource Pic}"/>
This works perfectly, the picture is displayed.
<Setter Property="IMDP" Value="{StaticResource Pic}"/>
This line drops a nullreference exception (IMDP is a legit dependency property of FrameworkElement type).
Noticed that ContentControl's Content property is an object so tried changing the dp type, tried FrameworkElement, object, Image, nullreference exception for all.
So Silverlight FrameworkElement dependency properties cannot be set from xaml?
Is this a bug? A known limitation? Is there a workaround?
Couldn't really find anything about it, only seen that it works in WPF.
Thank you
Sergey.L...
Contributor
7178 points
1,340 Posts
11-06-2009 4:59 PM |
Hi,
Hm.., strangely How do you declare a dependency property and how do you use it?
11-07-2009 10:00 AM |
Define this way:
public FrameworkElement Pic { get { return (FrameworkElement)GetValue(PicProperty); } set { SetValue(PicProperty, value); } } public static readonly DependencyProperty PicProperty = DependencyProperty.Register("Pic", typeof(FrameworkElement), typeof(ArenaUIWindow), null);
Set this way:
<Setter Property="Pic" Value="{StaticResource PicRes}"/>
Usage is irrelevant since when I set a breakpoint at OnApplyTemplate, it shows that the value is null, while in other cases where the dp is not a frameworkelement, they are all set.
BTW, as I wrote in my other question, I can set it the following way:
<Setter Property="Pic"> <Setter.Value> <Image Width="40" Height="40" Source="/ArenaUIWindowAssembly;Component/Images/pic1.png"/> </Setter.Value> </Setter>
Problem with this method is that I can't add it to a canvas, I believe due to the reason described here:
http://forums.silverlight.net/forums/p/117697/264939.aspx
11-07-2009 5:44 PM |
Can do you show a definition of PicRes resource?
11-07-2009 6:26 PM |
<Image x:Key="PicRes" Height="40" Width="40" Source="/ArenaUIWindowAssembly;Component/Images/pic1.png"/>
(defined in the same resource dictionary as the controltemplate)