Click here to Skip to main content
15,878,945 members
Articles / Expression blend
Tip/Trick

Attached Dependency Properties in SilverLight 3/4

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
27 May 2010CPOL2 min read 16.9K   2   2
A tip to overcome limitations in Blend 3 and Blend 4 RC when dealing with attached dependency properties.
While working to help out Alan Beasley with his request for 'developer support' on his article Picture Frame[^], I ran across a few things in Silverlight 3 and 4 and Blend 4 RC that are worth mentioning.

Silverlight 3


SL 3 has a bug in how it parses out Xaml that leaves you with little to no information as to the true source (see here[^]).

This can be 'somewhat' overcome with the solution provided in the link above by using xmlns (with assembly) at the root element of the control template, ex:
XML
<ControlTemplate TargetType="ContentControl">
  <Grid x:Name="LayoutRoot" xmlns:y="clr-namespace:MyNamespace; assembly=MyAssembly">
    <Path Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(y:MyClass.VerticalGradient)}" />
  </Grid>
</ControlTemplate>


For those who do use the above, it still causes some issues with the Blend designer, as Alan informed me. But otherwise, it will still compile and run just fine.

I did though find another option that takes advantage of the XamlReader. I cannot personally vouche for this approach, as I did not try it, but it can be found here[^] posted by thejvk.

Silverlight 4


SL 4 does indeed resolve the bug mentioned above in allowing you to neglect applying the "xmlns" element to the root of the control template. So, the code snippet would now look like this:
XML
<ControlTemplate TargetType="ContentControl">
  <Grid x:Name="LayoutRoot"
    <Path Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(y:MyClass.VerticalGradient)}" />
  </Grid>
</ControlTemplate>


Blend 4 RC (4.0.20421.0)


While working with Blend 4 RC (4.0.20421.0), I found an interesting bug. When you open a solution that does not have the class which defines the dependency compiled, Blend will not show the dependency property until you compile and then reopen the solution.

Once reopened, Blend will interpret any changes made to the Dependency property and update the UI as soon as the changes are built.

Bonus Tip


When attempting to make your UI designer's life easier, you'll want to apply the attributes BrowsableAttribute[^], CategoryAttribute[^], and DescriptionAttribute[^] to the GetXXX method of your dependency property. This method is the method of choice that Visual Studio and Blend look at when pulling information about your dependency property (see the remarks of this[^]).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralThanks again for all your help Andrew Pin
Alan Beasley27-May-10 23:20
Alan Beasley27-May-10 23:20 
GeneralRe: Thanks again for all your help Andrew Pin
Andrew Rissing28-May-10 3:41
Andrew Rissing28-May-10 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.