DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. My Experiences in Silverlight 4 : Writing re-usable Controls

My Experiences in Silverlight 4 : Writing re-usable Controls

Sudheendra Kovalam user avatar by
Sudheendra Kovalam
·
Apr. 05, 11 · News
Like (0)
Save
Tweet
Share
5.22K Views

Join the DZone community and get the full member experience.

Join For Free

these days i am working on silverlight and creating custom controls which would be re-usable in another projects. and when you set out to write re-usable silverlight controls, silverlight toolkit is the best code to look up to.

when you download the silverlight toolkit. the toolkit installer also copies source code of the toolkit at           “%program files%\microsoft sdks\silverlight\v4.0\toolkit\apr10\source” folder. when you start exploring the source code, you will notice a peculiar way the source code for toolkit is arranged. every control in toolkit is written as a content control and has a resource dictionary that has a default style.

image

so, inspired by this style of writing custom controls, i set out. now the reason why you want write a custom control is you require certain custom fields and a custom style and behaviors. so when you define these controls and the style, you also want your custom properties to appear in your style(or else why would you define those additional controls. typically your style for your control would look something like this.

and the your custom control would look something like this.

<resourcedictionary   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:vsm="clr-namespace:system.windows;assembly=system.windows"
  xmlns:layoutprimitivestoolkit="clr-namespace:system.windows.controls.primitives"
  xmlns:layouttoolkit="clr-namespace:system.windows.controls">

  <style targettype="my:customcontrol1">
    <setter property="foreground" value="#ff000000" />
    <setter property="borderbrush" value="#ffeaeaea"/>
    <setter property="horizontalalignment" value="left" />
    <setter property="template">
      <setter.value>
        <controltemplate targettype="my:customcontrol1">
          <grid x:name="root">
            <vsm:visualstatemanager.visualstategroups/>
            <border
              	x:name="border"
              	borderthickness="{templatebinding borderthickness}" padding="{templatebinding padding}" background="{templatebinding background}"
              	borderbrush="{templatebinding borderbrush}">
						<textblock text={templatebinding mycustomproperty1}
              </border>
          </grid>
        </controltemplate>
      </setter.value>
    </setter>
  </style>
</resourcedictionary>

and your .cs file would look something like this. notice that there is a peculiar way i chose to name the dependency property. you might want to follow this pattern (i.e. your dependencyproperty should be named your variabale name suffixed with “property”.

you must adhere to this naming convention if you intend to use blend in your project. (if your dependency property is named otherwise, you might not be able to use blend to set these template bindings.)

  public class mycustomcontrol1 : contentcontrol
    {

			public string mycustomproperty1
        {
            get { return (expanddirection)getvalue(mycustomproperty1property); }
            protected internal set
            { setvalue(mycustomproperty1property, value); }
        }

        public static readonly dependencyproperty mycustomproperty1property=
                dependencyproperty.register(
                        "mycustomproperty1",
                        typeof(string),
                        typeof(mycustomcontrol1),
                        new propertymetadata(expanddirection.down, onexpanddirectionpropertychanged));
...

now my control should show the value i set in mycustomproperty1 on the ui right?. something like this:

<grid x:name="layourroot">
	<my:customcontrol1  mycustomproperty1="hello content control"/>
</grid>

but this does-not seem to happen. after searching for sometime on silverlight .net forums , i could not exactly find out what i was doing wrong here. after some binging on bing and binging on google smile with tongue out . i found that for custom properties, templatebinding doesnot seem to work the way i expect it to work.

so instead as per the suggestions on the silverlight .net forums , i modified the template of the control by replacing templatebinding. now my textblock markup looks something like this.

<textblock text={binding relativesource={relativesource templatedparent},path=mycustomproperty1} />

now my template binding works as i expect it to work. essential what has changed in the previous representation and the new representation is still a matter of research for me. will keep posting more about this as and when i discover more about it.

if i have made any mistakes, feel free to write comments about what i am mistaken about. i’ll happily correct it :)


code style Property (programming) Template Dependency Binding (linguistics) Google (verb) Dictionary (software)

Published at DZone with permission of Sudheendra Kovalam. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes vs Docker: Differences Explained
  • Taming Cloud Costs With Infracost
  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Deploying Java Serverless Functions as AWS Lambda

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: