Home » Silverlight » Silverlight – Wire up your form for programming

Silverlight – Wire up your form for programming

As I mentioned a couple of weeks ago, learning Silverlight is a lot more about relearning some basic assumptions than it is about learning a new language.  We’ve already looked at the basic layout managers available and how that impacts putting a screen together.  Today we want to look at how we can capture events and access the controls from our CSharp code.

If you are already familiar with ASP.NET development, I think you will be surprised at how similar XAML coding looks to ASP.NET.

The first thing you might have trouble finding when you first try to wire up a control is how to access it by an identifier.  In ASP.NET, we’d use the ID attribute.  In Windows Forms, we’d use the ID attribute (unless we were in VB.NET where we’d use Name).  In XAML, we use the x:Name attribute.  Huh?

So if you want to just code an element’s identifier by hand, your XAML code will look something like this:

n style=”color: blue;”><UserControl xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data” xmlns:basics=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls” x:Class=”SilverlightApplication1.Page” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Width=”390″ Height=”382″ Background=”#FFE1BABA”> <StackPanel Height=”Auto” Width=”Auto”> <TextBlock x:Name=”m_textBlockLabel” Height=”Auto” Width=”Auto” Text=”TextBlock” TextWrapping=”Wrap”/> <Button Content=”Button” x:Name=”m_buttonPushMe”/> </StackPanel> </UserControl>

But we don’t want to have to do this by hand.  So instead, we can fire up Expression Blend 2.0 and use the properties window on the right to fill it in:


Much easier.

Now the next thing we’ll want to do is to wire up some events.  Again, you could just type it into the XAML, but once again, it will be easier if you use expression blend.

You’ll notice that just to the right of the  Name field in the properties is a lightning bolt icon.  Click that to get to the events that are available.  Unfortunately, there is no double click support to automatically create the event, but you probably already know the drill.  Just name it using the format controlName_EventName.  Save your code and go back to Visual Studio and reload the file and you’ll see that you have an event handler in your CSharp code file.

style="color: blue;">private void m_button_Click
    (object sender, RoutedEventArgs e)


All that’s left now is doing something in that event.  So let’s set the Text property of our TextBlock (kind of like a Label in ASP.NET or WindowsForms).

span style=”color: blue;”>private void m_button_Click
    (object sender, RoutedEventArgs e)
    m_textBlockLabel.Text = “Button was clicked”;


See, from here it looks a lot like code we’ve been writing in ASP.NET or Windows Forms.  Not a whole lot new.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Silverlight – DatabindingSilverlight – Databinding When I was learning DataBinding in both .NET 1.0 and .NET 2.0, I quickly discovered that most of what I learned about DataBinding for ASP.NET was useless as I moved to Windows Forms and […]
  • Silverlight – Binding ResourceDictionariesSilverlight – Binding ResourceDictionaries In the last two Silverlight posts I've introduced databinding in Silverlight.  We've seen that databinding, while similar to what we've experienced in Windows Forms and ASP.NET […]
  • More on SilverLight Developer InstallationMore on SilverLight Developer Installation If you followed along with the install instructions last week and then went to do some development, you probably noticed that the Visual Studio environment does not allow for WYSIWYG/Drag […]
  • Silverlight – Navigating DataSilverlight – Navigating Data Last week I demonstrated how to access data from a web service in Silverlight and display it on the page.  Today we are going to continue on with that demo and look at how to […]
  • Silverlight – Databinding to a Web ServiceSilverlight – Databinding to a Web Service In the last several posts, I’ve demonstrated various ways of getting data onto the screen.  What I’ve purposely omitted up until this point is how to update this information. So […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback