DZone
Mobile Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > XAML Dev’s Favorite Snippet

XAML Dev’s Favorite Snippet

Jerry Nixon user avatar by
Jerry Nixon
·
Dec. 25, 11 · Mobile Zone · Interview
Like (0)
Save
Tweet
5.97K Views

Join the DZone community and get the full member experience.

Join For Free

Data binding in XAML is powerful. We can wire up forms without code-behind. However, for it to work, data bound objects must implement INotifyPropertyChanged. Without this interface, object property value changes are not reflected in the UI.

Objects implement properties two ways. The first method (use the snippet “prop”) is to simply create the property. We’ve all done it. It’s like this:

image

There is nothing wrong with this. We’ve used it for years. However, it doesn’t support XAML data binding. XAML data binding requires INotifyPropertyChanged. This approach (there is no snippet to use) is to raise an event when the chance occurs, like this:

image

Because we raise the event in the setter, we can’t use simplified syntax. And because there is so much typing, I am annoyed every time I write one. Why is there no snippet for this? It doesn’t matter. Let’s just make one!

Step 1 Create a New XML File

Visual Studio conveniently creates files without requiring you to save them first.

image

Step 2 COPY the Snippet Template

Give the XML file focus. Insert a snippet. The keyboard shortcut is CTRL+K, CTRL+B. From the menu select “Snippets” and “Snippet”. This will inject the snippet template.

image


You can update the orange areas and create your snippet.  For more information on creating your own snippet, look here. This is the basic template:

image


But I want to save you time. So, I write the snippet for you. You just need to copy it into your file. But, notice <Code Language=”CSharp”> This tells Visual Studio where to make the snippet available. If you make your own, remember to update this value.

Here is my snippet for you:

image
image
image

Get the real code here.

Step 3 Add the Snippet to Visual Studio

Once you have the code copied the snippet, save the file with a .snippet extension. Then use (CTRL+K, CTRL+B) to open the Snippet Manager.

image


  1. Change the Language to Visual C#
  2. Click Import and find your file
  3. Click Open and Finish

Congratulations

You now have the snippet and are ready to use it. To use your new snippet:

  1. Go to a C# file
  2. Type “propnote”
  3. Type TAB+TAB
  4. Fill in the blanks, like this:

image


Source:  http://blog.jerrynixon.com/2011/11/xaml-devs-favorite-snippet.html


Snippet (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • No-Code/Low-Code Use Cases in the Enterprise
  • Hard Things in Computer Science
  • SQL vs. NoSQL: Pros and Cons
  • Monolith vs Microservices Architecture: To Split or Not to Split?

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo