Click here to Skip to main content
13,254,077 members (61,307 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

29.1K views
2 bookmarked
Posted 18 Jan 2012

How to simplify coding DependencyProperty in WPF and Silverlight

, 11 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Simplify Coding DependencyProperty
Introduction

This tip will introduce an excellent method to simplify coding DependencyProperty in WPF and Silverlight.

Background

Writing Dependency Property is really an annoying task and it is so easy to make mistakes. I've found an excellent solution to solve this problem.

Using the code

  1. Install AutoCode 4.0 (http://www.devprojects.net/).

  2. Generate the three files at the last section of the tip (contains the custom script written by myself) and copy it to "C:\Documents and Settings\user\My Documents\AutoCode 2010\Commands\CSharp\Misc".

  3. Open Visual Studio and find a proper place to type "<propertyType> <propertyName> <ownerType> dp", press "Ctrl + Enter", and this line will be replaced by a generated section of code, which is exactly what you want.

Any problem, please feel free to ask me and wish you enjoy it!

PS: Professional Edition of AutoCode is required to use the custom script without any limitation, or else you need to restart Visual Studio after you have used it more than four times. Refer to http://www.devprojects.net/donate to find how to Activate AutoCode Professional Edition.

Points of Interest
You can do a lot of other great things with AutoCode to simplify our daily development work.

dependencyproperty.autox
<?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v4.0">
  <Command name="DependencyProperty" version="1.0">
 
    <Includes>
      <Include file="DependencyProperty.cs" />
    </Includes>
 
    <CommandBehavior>
      <CommandLine shortcut="dp" />
      <ActiveDocument extensions=".cs"/>
    </CommandBehavior>
 
    <CommandInfo>
      <LanguageCategory>CSharp</LanguageCategory>
      <Category>Code</Category>
      <Usage>
        <![CDATA[<propertyType> <propertyName> <ownerType> dp]]>
      </Usage>
      <Example>RevealedImageData ImageData ThumbnailItem dp</Example>
      <Description>Constructs a dependency property.</Description>
      <Author>Logan</Author>
      <HelpUrl>http://help.devprojects.net/gallery/command/dependencyproperty</HelpUrl>
    </CommandInfo>
 
    <CommandCode language="csharp">
 
      <Codes>
        <Code id="Template" file="DependencyProperty.txt" />
      </Codes>
 
      <Selection codeElement="Template" codePoint="StartOfElement" clearSelection="true">
        <SelectText>/*I*/</SelectText>
      </Selection>
 
      <SmartFormat>
        <Start codeElement="Template" codePoint="StartOfElement" />
        <End codeElement="Template" codePoint="EndOfElement" />
      </SmartFormat>
 
    </CommandCode>
      <Signature><![CDATA[
      TywprO0zpyigJnvnfRLr5DQzFqx/waZe+PltR8oKmS40caftX06mJ+jTb
      HRDdg2aRWFfQWhaJnzj9FNHblputjFrOFp8BeEGpYcawV52uQoUBgQ3xF
      AE9D0BXG/m542oPjQnpr7L92oMYRBwXOS1air+r2aRqM4AiPxcDDf4DXU=
    ]]></Signature>
 
</Command>
</Commands>


dependencyproperty.cs
using System;
using System.IO;
using System.Collections.Generic;
 
using DevProjects.AutoCode.Commands;
 
public partial class DependencyPropertyCommand : CommandBase
{
    private string PropertyType;
    private string PropertyName;
    private string OwnerType;
 
    protected override bool BeforeExecute(string commandArgs)
    {
        if (Arguments.Length != 3)
        {
            CommandHelper.InvalidArguments("Please, specify property type, property name and the owner type.");
            return false;
        }
 
        PropertyType = Arguments[0];
        PropertyName = Arguments[1];
        OwnerType = Arguments[2];
 
        return true;
    }
}


dependencyproperty.txt
public <#=PropertyType#> <#=PropertyName#>
{
    get { return (<#=PropertyType#>)GetValue(<#=PropertyName#>Property); }
    set { SetValue(<#=PropertyName#>Property, value); }
}
public static readonly DependencyProperty <#=PropertyName#>Property =
    DependencyProperty.Register("<#=PropertyName#>Property", typeof(<#=PropertyType#>),
    typeof(<#=OwnerType#>), new PropertyMetadata(On<#=PropertyName#>Callback));
private static void On<#=PropertyName#>Callback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    <#=OwnerType#> <#=ToCamelCase(OwnerType)#> = obj as <#=OwnerType#>;
    <#=PropertyType#> <#=ToCamelCase(PropertyType)#> = e.NewValue as <#=PropertyType#>;
 
    if (<#=ToCamelCase(PropertyType)#> == null)
        return;
 
    <#=ToCamelCase(OwnerType)#>/*I*/ = <#=ToCamelCase(PropertyType)#>;
}

License

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

Share

About the Author

DawningTiger
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
sgmoore15-Jun-13 2:07
membersgmoore15-Jun-13 2:07 
GeneralReason for my vote of 1 I don't see the point, just use prop... Pin
MLaino15-Feb-12 7:17
memberMLaino15-Feb-12 7:17 
GeneralThis is not simpler than Ctrl+C, Ctrl+V. Real "simplificatio... Pin
Member 308248723-Jan-12 22:03
memberMember 308248723-Jan-12 22:03 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 11 Feb 2012
Article Copyright 2012 by DawningTiger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid