WPF Data Binding in VS 2010


Till VS2008 programmers have to write binding statements mostly directly in XAML – otherwise we have to use Expression Blend. Cider/WPF designer in Visual Studio 2008 was not powerful enough for handling Data binding. Visual Studio 2010 (Beta 1) has powerful Data Binding tooling mechanism in-built much like Expression blend.

Let’s walk step by step of how to use Data Binding tools in VS 2010. We have a simple WPF window like this –

<StackPanel>

        <Label>Enter your name:</Label>
        <TextBox x:Name="textUser"></TextBox>
       
<Label>Name entered by you is:</Label>
       
<TextBox x:Name="txtMessage"></TextBox>

</StackPanel>

 

To databind the lblMessage label with text textbox select the label in window and press F4 or select properties. In the properties windows select Content property and select the “Advanced Properties” icon (newly added in VS 2010). In the context menu select “Apply Data Binding” option

 

In the New Data Binding window select ElementName tab (as we have not set any DataContext). We want to bind it with another element which in this case is the text box. RelativeSource and StaticResource tabs are used for Relative Source and Static Resources.

 

Select Path tab and select Text property, as we want to bind to the Text property of txtUser source text box control.

 

If we want to use Converter, we can use Converter tab – in this case we are not going to use one. Options tab allows to set Data Binding options like Mode and UpdateSourceTrigeer. In this example we are setting Data Binding Mode as Two Way (so that updating Target TextBox also updates Source). We are also setting UpdateSourceTrigget property as PropertyChanged so that changes happen at key change event for text box.

 

Once done watch the Text property of the txtMessage text box –

VS 2010 IDE has set it as –

<TextBox x:Name="txtMessage" Text="{Binding ElementName=txtMessage, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

This is the same we would’ve done in VS 2008 / XAML manually.

One more interesting info here – similar to many property window/grid Data binding window also uses Reflection heavily – once we specified Source Element, it used Reflection to get a list of all the properties of the control to bind.

Advertisements

VS 2010 and .NET Framework 4 – Beta 1


I installed VS 2010 and .NET Framework Beta 1 a month ago, but did not get time to write about it as I was busy exploring it. You can download VS2010 and .NET Framework 4 (Beta 1) from here – http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx – also the page includes video on how to download and install the beta.

One of the first thing you would notice after opening VS 2010 is the new look & feel of the IDE –

 

One of the much talked about things about Visual Studio 2010 is the code editor of VS 2010 is WPF based. So holding the CTRL button and pressing mouse middle button up and down actually does a zoom in / zoom out –

 

 

This feature indeed uses the ScaleTransform feature of WPF with ScaleX/Y set from the mouse input. Also VS 2010 has a nice looking New project dialog that allows multi-targeting (multi-targeting of different .NET Frameworks introduced in VS 2008) and searching templates.

 

Visual Studio now also comes with Team Explorer (client for Team Foundation Server/TFS) which required additional install earlier. This is nice as you get TFS client out of the box.

 

 

The connect to Team Project dialog has also undergone change and has a new look & feel now. It’s possible to group couple of Team Projects together under a bucket – called Team Project Collections or Directory. A Team Project Collection or Directory can contain multiple Team projects – this feature allows logical grouping and maintenance of team projects. A TFS Server can have multiple Team Project Collections and each Team Project Collection in turns can have multiple Team Projects.

 

Brian Harry has a detailed blog about the new feature – http://blogs.msdn.com/bharry/archive/2009/04/19/team-foundation-server-2010-key-concepts.aspx

VS2008

VS 2010

These are merely just the tip of the icebergs as both VS 2010 and .NET Framework 2010 have undergone tons of changes – I would blog about some of the changes in later blog posts.

Note: F# is released as a product in VS 2010 release and comes out of the box. Select View > Other Windows > F# Interactive to open F# interactive and key in few code snippets. Nice!

Small Basic – A Fun way of Programming


 

Though normally now a days I mostly use C# (with few exception of VB.NET), I used to code in Visual Basic for a long period of time starting from VB4 to VB6. So I like Basic programming language and it’s syntax. With the introduction of .NET 4, one of the core theme for both C# and VB.NET programming language is to bring the languages closer to each other and narrow down the gap between these two languages. So both C# and VB.NET would start support features supported by their counter parts like automatically implemented properties.

Though when you are coding in .NET whether you’re coding in C# or VB you’re mostly using lot of BCL classes, syntaxes do matter to a programmer (in my humble opinion).

Microsoft Dev Labs had earlier released a fun programming language called Small Basic – as you can guess from the name it’s based on Basic and it’s a very small subset of Basic programming language. Since it’s intended audience is hobbyists and students, the creators of Small Basic have carefully avoided making the language bigger – so the language is very simple (it has not more than 20 keywords).

Small Basic (which could be downloaded and installed from Dev Labs site) comes with a compiler and an IDE for coding and running the program. Though it’s still does not support debugging (I am currently using version 0.2), probably debussing would be available in next version. Small Basic comes up with a nice yet simple WPF based IDE – 

 

As you type code, Visual Studio like Intellisense and contextual help about the API (class/method/variable) you use would be displayed in help window on the right. Small Basic comes with a small library and keywords.

Small Basic is very simple and easy to use – it does not have any notion of data type or type system or strong type system. Variables could be defined as variable = val (e.g. x = 1, y = 2). You also don’t have to instantiate classes – all the classes behave as static classes with static methods.

It supports Basic like keywords – If Then, Else, End If, For, End For, While etc

To show output in Console TextWindow.Write or TextWindow.WriteLine method is used

To show graphics window GraphicsWindow.Show method is used.

There are some interesting classes like Turtle, Flickr, Dictionary – I would not tell how they are used – rather let you figure that out. The install also comes with a PDF help file for quick reference and learning.

Excellent – Great Work by SB team.

Note: I could not restrain myself from showing what is Dictionary and how to use it. If you are coming from C# or other complex programming languages you would be tempted to think of Dictionary as a data structure (that stores key, value pair), but it’s not so. It’s actually what it literally means – Dictionary class helps to get the meaning of any word. Now you get it – yes it’s that simple. Simplicity is the hidden power of Small Basic. Here’s a program on how to use Dictionary class. Happy coding – 

TextWindow.WriteLine("Enter a word")
word = TextWindow.Read
defintion = Dictionary.GetDefinition(word)
TextWindow.WriteLine(defintion)