Best Practices Development for Silverlight - MVVM

Posted on: Wed Jun 09 15:06:32 -0700 2010. Updated on: Thu Jun 10 16:51:14 -0700 2010.
Category: Silverlight

If you're using silverlight to develop apps, you "should" follow the MVVM pattern, ie Model-View ViewModel. It is basically the pattern for databinding between your data Model and the View. Remember, it is just a recommended pattern, and everything else are helpers. It is OK to have code in your code-behind, but this should be kept to a minimal, and ideally most stuff of your business logic is abstracted out to the model.

Here is a great link to get started and learn more: MVVM Light Toolkit
I very much recommend watching his video from MIX 2010.

Changing Visual State from Codebehind

Posted on: Thu Jun 19 11:35:32 -0700 2008. Updated on: Sat Jun 21 00:36:53 -0700 2008.
Category: Silverlight

Lets say you want to change the Visual state of a control, for example button1. In the codebehind you would go:

VisualStateManager.GoToState(button1, "MouseOver", true);
Notes: You can only set the visual state of a control that is of control type, ie a Button has visual states but a Grid or TextBlock does not. I know that is lame...

Accessing Controls in a Template to Databind Properties

Posted on: Wed Jun 18 16:12:49 -0700 2008. Updated on: Sat Jun 21 00:23:22 -0700 2008.
Category: Silverlight

So, lets say that you created a silverlight button control and you are overriding the default template with your own defined template. Now, let's say you need to databind, say, the TextBlock text property on an event, but you cannot acces the TextBlock directly because it is within a template. Solution: Simply set the TextBlock Text property to:

{Binding ObjectPropertyName}
and then in the code behind in your event that will bind the data, go:
Button.DataContext = ObjectWithDefinedProperties;

Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.