Page view counter

Dependency Properties - Precedence

Today we wrap up dependency properties by talking about one of the most important, though typically invisible aspects: DP precedence order. This is critical because dependency properties, by their nature, are designed to have their final value set from more than one influence (e.g., resources, data binding, animation, etc.)

The rule is rigid, sensible and straight forward…

Animation uber alles.

If there is an animation affecting the value, that will take precedence over all other values

Local values over everything but Animation

In the absence of an animation, use the local value to override any other values

Templates/Styles if no animation or local values

If there is no animation or local value, use the value from the template, and if there is no template then from the style.

When all else fails, use the default

If none of the above apply, use the default value for that type.

The Default Value

It is the last rule that demands that every type have a default look, and it is because of that rule that you crated generic.xaml – so that if your custom control has a value that is not controlled by an animation, a local value, a template or a style, the Dependency Property System will turn to the default values in generic.xaml and use those.

Our First Application

With this, we're ready to examine the code to our first application.  As you may remember, we have three projects in one solution.  In CustomControl.cs we declare the visual logic of our control and we will, eventually declare the logic (event handlers, etc.) as well. For now, the listing is quite simple:

   1: using System.Windows.Controls;
   2:  
   3: namespace ClassLibrary
   4: {
   5:    public class CustomControl : Control 
   6:    {
   7:       public CustomControl()
   8:       {
   9:          DefaultStyleKey = typeof( CustomControl );
  10:       }
  11:    }
  12: }

The simplest logic you can add to get the default style to show. The default style, as you remember is in the file generic.xaml which in turn is in the Themes folder,

   1: <ResourceDictionary
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     xmlns:controls="clr-namespace:ClassLibrary" 
   5:     xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
   6:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   7:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   8:     mc:Ignorable="d">
   9:     <Style TargetType="controls:CustomControl">
  10:         <Setter Property="Template" >
  11:             <Setter.Value>
  12:                 <ControlTemplate TargetType="controls:CustomControl">
  13:                     <Grid x:Name="LayoutRoot">
  14:                         <Ellipse x:Name="Core" 
  15:                             Width="75" Height="75" 
  16:                             Stroke="Blue" StrokeThickness="2" >
  17:                             <Ellipse.Fill>
  18:                                 <RadialGradientBrush>
  19:                                     <GradientStop Color="#FFE0E1F0" Offset="0.004"/>
  20:                                     <GradientStop Color="#FF8080B1" Offset="1"/>
  21:                                     <GradientStop Color="#FF05052B" Offset="0.911"/>
  22:                                 </RadialGradientBrush>
  23:                             </Ellipse.Fill>
  24:                         </Ellipse>
  25:                     </Grid>
  26:                 </ControlTemplate>
  27:             </Setter.Value>
  28:         </Setter>
  29:     </Style>
  30: </ResourceDictionary>

Remember to set the BuildAction property of this file to Resource

SetAsResource

Page.xaml will create an instance of your custom control and an instance of a TextBlock

   1: <UserControl x:Class="SkinnableCustomControl.Page"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     xmlns:Controls="clr-namespace:ClassLibrary;assembly=ClassLibrary"              
   5:     Width="400" Height="300">
   6:     
   7:     <Grid x:Name="LayoutRoot" Background="White">
   8:         <Grid.RowDefinitions>
   9:             <RowDefinition Height=".7*" />
  10:             <RowDefinition Height=".3*" />
  11:         </Grid.RowDefinitions>
  12:         <Controls:CustomControl x:Name="CustomCtrl" Grid.Row="0" />
  13:         
  14:         <TextBlock Text="Skinnable Custom Control" 
  15:                    FontFamily="Georgia" FontSize="18" 
  16:                    HorizontalAlignment="Center" Grid.Row="1" />
  17:     </Grid>
  18: </UserControl>

When you run this, the custom control is drawn (along with the text block)

SkinnableCustomControlRunning

At the moment it doesn't respond to visual events such as mouse over, nor does it have any logic to handle clicks, but it does draw and it does have a default appearance. It exists (to a minimal degree) within the Parts and States model, and it is pretty straight forward from here to add the missing bits.

Next time.

 

-jesse

Published Thursday, October 02, 2008 1:39 PM by jesseliberty

Comments

# re: Dependency Properties - Precedence

Remember I said I'd post by 1pm? Okay, maybe I should have said every day and let it go at that. :-)

Thursday, October 02, 2008 1:40 PM by jesseliberty

# dependency properties precedence

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, October 02, 2008 11:57 PM by DotNetKicks.com

# 2008 October 03 - Links for today &laquo; My (almost) Daily Links

Pingback from  2008 October 03 - Links for today &laquo; My (almost) Daily Links

# Silverlight news for October 3, 2008

Pingback from  Silverlight news for October 3, 2008

Friday, October 03, 2008 6:53 AM by Silverlight news for October 3, 2008

# Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew

Friday, October 03, 2008 9:42 AM by Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew

# Silverlight Cream for October 03, 2008 -- #387

In this issue: Denislav Savkov, Martin Mihaylov, Matthias Shapiro, Mike Snow, Terence Tsang, Jesse Liberty

Friday, October 03, 2008 3:48 PM by Community Blogs

# re: Dependency Properties - Precedence

This dependency properties concept is very interesting. Are you planning to create any video about it?

Thanks,

Rachida

Saturday, October 04, 2008 9:59 PM by rachidadukes@live.com

# re: Dependency Properties - Precedence

rachidadukes] This dependency properties concept is very interesting. Are you planning to create any video about it?<<

Absolutely. I’ve started a series of videos on Custom controls and one in the series will cover DPs in depth.

Thanks!

-j

Saturday, October 04, 2008 11:00 PM by jesseliberty

# Custom Controls – The Denouement

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

Thursday, October 09, 2008 9:48 AM by Jesse Liberty - Silverlight Geek

# Custom Controls – The Dénouement

Over the past month I've posted half a dozen min-articles about creating custom controls that can interact

Thursday, October 09, 2008 10:11 AM by Microsoft Weblogs

# Custom Controls – The Dénouement

Over the past month I&#39;ve posted half a dozen min-articles about creating custom controls that can

Thursday, October 09, 2008 10:38 AM by Mirrored Blogs

# Custom Controls - the handling at Blog von J??rgen Ebner

Pingback from  Custom Controls - the handling at Blog von J??rgen Ebner

Friday, October 10, 2008 6:15 AM by Custom Controls - the handling at Blog von J??rgen Ebner

# re: Dependency Properties - Precedence

Jesse,

Are you still going to continue this series of posts? I felt it was left in the middle of something important....

Monday, October 13, 2008 9:49 AM by mamadero2

# re: Dependency Properties - Precedence

Mamadero2... I'd be happy to but what area would you like to see covered?  Have you checked out the tutorial on databinding? That might cover the areas that you feel are missing.  

Monday, October 13, 2008 3:01 PM by jesseliberty

# re: Dependency Properties - Precedence

What about value inheritance? If I set FontStyle in UserControl, child elements inherits font style.

Thursday, October 16, 2008 5:27 AM by Martin Kruszynski

# Creating Skinnable Custom Controls - Video

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

Friday, December 05, 2008 3:01 PM by Jesse Liberty - Silverlight Geek

# Creating Skinnable Custom Controls - Video

&#160; &#160; I have completed the four part video series Creating Skinnable Customer Controls and I

Friday, December 05, 2008 3:52 PM by Microsoft Weblogs

# Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF &amp; .NET

Pingback from  Erstellen von anpassbaren Custom Control at Programming with Silverlight, WPF &amp; .NET

# Application Set: Templates & Custom Controls

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

Monday, December 22, 2008 5:34 PM by Jesse Liberty - Silverlight Geek

# Application Set: Templates & Custom Controls

When we started creating How Do I videos, the idea was to have stand alone videos that do not depend

Monday, December 22, 2008 6:17 PM by Microsoft Weblogs

# Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

Pingback from  Templates &#038; Custum Controls at Programming with Silverlight, WPF &amp; .NET

# D is for... Dependency Property

Dependency properties came on the scene with .NET 3.0 to support a variety of functionality in Windows

Monday, January 26, 2009 12:59 AM by Jim O'Neil's Blog

# Websites tagged "dependency" on Postsaver

Pingback from  Websites tagged "dependency" on Postsaver

Sunday, May 17, 2009 5:02 PM by Websites tagged "dependency" on Postsaver

# What’s New In Silverlight 3

&#160; &#160; I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and

Friday, May 29, 2009 4:57 PM by Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3

I had the pleasure of presenting What’s New In Silverlight 3 both at TechEd this year and then again

Friday, May 29, 2009 5:42 PM by Microsoft Weblogs

# Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in Silverlight 3

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in Silverlight 3

# Top-silverlight &raquo; Blog Archive &raquo; What???s New In Silverlight 3

Pingback from  Top-silverlight  &raquo; Blog Archive   &raquo; What???s New In Silverlight 3