Tag Archives: visual C# 2010 tutorials

The WPF Layout System – Visual C#

Layout in WPF is flow-based, but it also includes automatic resizing and repositioning of controls if the user changes screen resolution or resizes the window containing controls or adds or removes controls at runtime. As you will see in this chapter, this provides resolution and size independent user interfaces.

A window can contain one element, or control. That element will almost always be a container. A container contains controls and, optionally, other containers. All container controls derive from the System.Windows.Controls.Panel class. Layout in WPF is a recursive process. The .NET Framework runtime makes two passes before drawing elements.

In the Measure pass, the runtime queries each element to determine the element’s size and location. In the Arrange pass, the runtime determines the actual size and position for each element. Elements may or may not be sized and located where they requested, depending on the number of elements, the screen resolution, the size of the window, etc.

Layout is not only a recursive process, but it is also an iterative process. The entire layout process occurs each time the user resizes the window or your code adds or removes elements. You should keep this in mind as you design user interfaces.

How does the layout system determine how to size each element? Each element on a window is surrounded by a bounding box, as shown in Figure 1. This represents the space allocated to the element, and therefore represents the maximum size of the element.


Figure 1 Each element on a window is surrounded by a bounding box.

The layout system determines the actual size of an element by taking into account the following:

  •  The available screen space.
  • The size of any constraints, such as maximum or minimum height or width.
  •  Layout-specific properties, such as alignment, margins, padding, etc.
  •  The behavior of the parent container.

As needed, the layout system can shrink, grow, or move elements on a window.

Alignment, Margins, and Padding

All elements have four properties that are important to understand, as these play a key role in determining the position of elements in a window. These properties are HorizontalAlignment, VerticalAlignment, Margin, and Padding. Alignment refers to how child elements should be positioned within a parent element’s allocated layout space. The HorizontalAlignment property determines how an element is positioned horizontally within a container. The possible values for this property are Left, Right, Center, and Stretch, as shown in Figure 2. If this property is set to Stretch, which is the default, the control will expand to its maximum available width.


 Figure 2  The HorizontalAlignment property determines how an element is positioned horizontally within a container.

The VerticalAlignment property determines how an element is positioned vertically within a container. The possible values for this property are Top, Bottom, Center, and Stretch, as shown in Figure 3. If this property is set to Stretch, which is the default, the control will expand to its maximum available height.


Figure 3. The VerticalAlignment property determines how an element is
positioned vertically within a container.

The Margin property determines the distance between an element and its child or peers. This property consists of four parts: the left margin, the top margin, the right margin, and the bottom margin. The following XAML sets the margin for an element following this pattern:

<Button Margin=”0,10,0,10″…

The previous code specifies that there should be no additional space on the left and right, but there should be a ten pixel space on the top and bottom. If you want the same margin on all four sides, you only need to specify one value. The following XAML sets the margin to ten on all sides:

<Button Margin=”10″…

The Padding property is similar to the Margin property, however it is only available for certain controls, including Border, Button, and TextBlock. The property adds space between the edge of a control and its content.

Figure 4 shows the use of the Margin and Padding properties, along with horizontal alignment, to control the layout of elements.


Figure 4. You can use the Margin and Padding properties, along with horizontal
alignment, to control the layout of elements.


As mentioned earlier, each window can contain a single element. This element will typically be a container control. This chapter covers the following containers:

  • StackPanel: Stacks elements horizontally or vertically.
  • WrapPanel: Places elements in rows and columns that wrap as needed.
  • DockPanel: Aligns elements along an edge.
  • Grid: Places elements in rows and columns.
  • Canvas: Places elements according to coordinates.

This post is an excerpt from the online courseware Windows Presentation Foundation Using Visual C# 2010 course written by expert Ken Getz and Robert Green.

Ken Getz is a Visual Studio expert with over 25 years of experience as a successful developer and consultant. He is a nationally recognized author and speaker, as well as a featured instructor for LearnNowOnline.

Robert Green is a Visual Studio expert and a featured instructor for several of our Visual Basic and Visual C# courses. He is currently a Technical Evangelist in the Developer Platform and Evangelism (DPE) group at Microsoft. He has also worked for Microsoft on the Developer Tools marketing team and as Community Lead on the Visual Basic team. Robert also has several years of consulting experience focused on developer training, and is a frequent speaker at technology conferences including TechEd, VSLive, VSConnections and Advisor Live.

Domain Centric Modeling – Microsoft SQL Server 2012

When the first version of Entity Framework came out, it included support for database-first design, which would reverse engineer an existing database and build a model for you. You could build a model using the designer, but there was no tooling support for building a database from the model, so you had to perform that work yourself. The next version of Entity Framework, version 4, released with .NET 4 and Visual Studio 2010, added the necessary tooling support that made model-first design a viable option for using Entity Framework. Through these two releases, the database and model were the focus of early development work on a project. But if you follow domaincentered design, you probably found that Entity Framework really didn’t fit into the way you create applications.

All that changed with Version 4.1 of Entity Framework, released in the early part of 2011. This version added yet another option, code-first design. Codefirst design lets you create the domain model for your application by building Plain Old CLR Objects (POCOs), with no Entity Framework code whatsoever and without using any base classes. Then, through some clever features, you can create a context class that almost magically turns the objects into fully functional entity data objects. Even more amazingly, you don’t have to worry about creating the database at all. Assuming that you can live with the default conventions that you’ll learn about in this chapter, Entity Framework will create the database for you automatically when you first run the application.

The way that code-first works by default will probably work for most applications early in their development cycle. But when you’re ready to deploy the application to a production server, or need more flexibility in how Entity Framework creates a database, you can take advantage of data annotations on the model classes or use a fluent API to take control of the process. And even if you have an existing database that the application will use, you can still use code-first design and map the POCOs to the existing tables and fields.

The biggest benefit of code-first design is that it lets you focus on domaincentric modeling for application development, focusing on the data objects in your application rather than on the database or the Entity Data Model. In fact, your application won’t even have a model, and won’t have an .edmx file in the project, or any XML mapping code. Entity Framework takes care of inferring the model—including the conceptual, storage, and mapping models—at runtime. It really is a code-centric style of writing database applications.

Code-first design works so well and is implemented so nicely that we are confident that it will become the method of choice for new application development.

NOTE Code-first design is only available with Entity Framework 4.1 and later. This is an out-of-band release made available well after the release of Visual Studio 2010, .NET Framework 4, and Entity Framework 4. As a result you’ll need to download and install this release separately. See the course introduction for information.

Using Code-First

There are two different ways you can use code-first for development. The first, and what we expect will turn out to be the most common way, is to write data model .NET classes in your application first, then let code-first in Entity Framework create the database for you. This is the usage scenario that you will learn about in this chapter. Using code-first this way is the easiest, most agile way to jump-start database development, and lets you evolve and refactor the application in its early stages right through to production. Code-first can even update the database as you make changes to the model.

The other usage scenario is to again create the data model classes in your application, then map those data classes to an existing database. This will provide the information Entity Framework needs to provide data access between the application and the database, without the formality of an Entity Data Model. This might sound a little like database-first design, but the difference is that you create your own POCOs rather than having Entity Framework generate them from the Entity Data Model.

As you’ll learn later in the chapter, there are two ways in Entity Framework to map classes to objects in a database, no matter which usage scenario you use. One uses data annotations on the classes and properties of the data model classes, and the other uses the DbModelBuilder API to control the mapping in code.

ldn-expertdkielyThis post is an excerpt from the online courseware for our Microsoft SQL Server 2012 Developer course written by expert Don Kiely. 

Don Kiely is a featured instructor on many of our SQL Server and Visual Studio courses. He is a nationally recognized author, instructor and consultant who travels the country sharing his expertise in SQL Server and security.

Enjoy the Best of Both Worlds



Learning a technology does not have to be a stressful task that takes away from your daily life. While improving your level of education is important, and can be incredibly helpful with regards to securing or maintaining a job, commitments to family are also vital. It is possible to give time and energy to both education and your home life, however, without making problems in either arena. Online courses, such as a Visual C# 2010 tutorials, or C# .NET courses, can help you get a leg up at work, without feeling like you’re missing out on important personal commitments.

An online computer course allows you to view the material at your own pace, and to learn when it is most convenient for you. Rather than traveling to a class, the class comes to you, and study time can be whenever you have the time, and the energy, to get the most out of it. It is unfair to ask anyone to choose between family and work-related education. An online HTML5 tutorial can help you enjoy the best of both worlds, without feeling like you are about to collapse under the pressure.

Visual Basic vs Visual C# Smackdown



Visual C# is similar to Visual Basic, but it has some special features not found in Visual Basic. For example, it supports multi-line comments and static classes along with partial interfaces. Additionally, C# lets you block dangerous codes with a simple keyword.

Visual C# is a great language for developers who want to type less without having to spend much time learning how to use the software. A Visual Studio 2010 tutorial video can show you, step by step, how to master this program.

If you already know how to use C++ and Java, you will pick up Visual C# quickly with a comprehensive C# video course. There are several Visual C# 2010 tutorials available online, but the best course should offer the student videos made only by industry experts.

In a typical C# video course, you will learn how to utilize Windows Communication Foundation (WCF) to build connected, service-oriented applications or take advantage of the MVC (Model-View-Controller) framework to create advanced Web sites. You will also learn how to take advantage of some of the new features introduced in Visual C# 2010. Most video courses also include introductions to object-oriented programming, data manipulation, libraries, and manipulating graphics.

Check out a Visual Studio or Visual C# Video Course Today!