Tag Archives: Presentation Foundation Using Visual C# 2010

Windows Presentation Foundation Using Visual C# 2010: The Resources Dictionary

Every element in your application provides a Resources property (this includes every control and container), which is actually a Dictionary in which the key is a string, and the value is a resource. In other words, every element contains a collection of resources, indexed by a string key name.

Although every element can contain its own defined resources, most of the time it’s easiest to place resources at the window level. Doing that makes the available to all the controls in the window. Every element always has access to all its resources, and the resources of its parents (all the way back up to the application level), so if you want to share resources between elements, the higher you place the resources, the easier it will be to do so.

Imagine that you want to create a LinearGradientBrush instance that you will use as the background for several controls on a page. Rather than defining it in the content of each individual element, you can extract the gradient in the resources collection for the container (in this example, a user control) instead. The sample user control, IntroResource.xaml, includes a very simple resource. The syntax in the sample (displayed in Figure 1) looks like the following:

gradient_figure1Figure 1. The sample gradient.

TIP: Although the examples here are all based on user controls for the purposes of making the demonstration easier to navigate, you can also define resources at any level. Often, in WPF applications, you’ll define resources at the window level, in the Window.Resources element.

The differences between this definition and what you would see if you had defined the gradient brush for each individual element are:

  • The resource appears inside the UserControl.Resources element.
  • The resource includes an x:Key attribute, defining the key for the resource. Remember that the collection of resources is actually a Dictionary, and every item in a dictionary must contain a unique key that identifies it.

NOTE The x: prefix on the Key attribute indicates that it’s in the XAML  namespace, rather than the WPF namespace. You’ll use this key to refer to the resource, whenever you want to use it. It doesn’t matter what you call the resource, as long as you use this Key attribute value to refer to the resource.

TIP: Name your resources based on their functionality, not their contents. The uses won’t change as often as the particular implementation details, and it will be easier to select resources if you name them based on their usage.

To use the resource, you must be able to refer to it. To do this, use a markup extension, delimited with curly braces:

You may have seen the markup extension syntax used when performing property binding, or in other contexts. In general, XAML uses markup extensions whenever it needs to be able to set a property in a non-standard way. In this case, the StaticResource keyword indicates that when XAML needs to retrieve and set the Background property of the Border element, it
should look for a resource whose x:Key attribute is BackgroundBrush, and use
the definition it finds there.

Would you use a resource in a case like this, in which you only consume the resource once? It certainly doesn’t make the XAML any more efficient, or more compact. On the other hand, it does make it easier to isolate the layout details—by placing all the formatting information in one place, it’s easier to keep track of, and maintain, the look of your application.

You may be wondering about the word “Static” in the markup extension. Why Static? Is there a Dynamic resource, as well? WPF provides both static resources, which are applied only once, when you first create the window, and dynamic resources, which are reapplied if the resource changes. Dynamic resources (using the DynamicResource markup extension) use slightly more overhead, but allow you to change the content of the resource at runtime. Static resources are simpler, and provide much of the functionality you’ll need.

NOTE If you programmatically change a feature of an existing static resource at runtime (for example, if you change a color, or some other property of the existing resource), WPF automatically updates the display to match. Dynamic resources allow you to replace the entire resource with another at runtime. In general, static resources provide the functionality you’ll need for most applications.

WARNING! It’s important that you define the static resources before you attempt to use them in the markup. Unlike most modern programming languages, the order of appearance within the source file is a concern—you cannot use a resource before it has been declared. The XAML parser reads content from start to finish, and expects to find the definition for a resource at the time the XAML attempts to use the resource. This isn’t true for dynamic resources, however. You can define dynamic resources after they’re used, and the XAML parse can still find them.

This post is an excerpt from the online courseware 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.

Radio Button Control – Visual C#

The RadioButton control works much like the CheckBox control—it provides the same properties, and the same events. The big difference between the RadioButton and CheckBox controls is that RadioButton controls are grouped by their container. Within a single container, you can generally only select one RadioButton control. If you place multiple RadioButton controls into multiple containers, you can select one RadioButton control from each container.

You can override this behavior using the GroupName property of the control. You can set this string value to create more than one group per container, or to create a single group that spans multiple containers. To demonstrate this behavior, select the RadioButton tab in the sample application, shown in Figure 1.

TIP: There’s no need to specify a GroupName property for a RadioButton control unless you want to override the default behavior.

radiobuttoncontrl

Figure 1. RadioButton controls normally allow you to select only one per
container; override the behavior with the GroupName property.

The markup for the sample page looks like the following:

If you try out the demonstration, you’ll see that you can only select a RadioButton control from the group named Group 1, because they’re all in the same container. You can select only a single item from the group named Group 2, again, because they’re in the same container. You can also select one item from the group named Group 3, because these items have their GroupName property set to the same text (the text is arbitrary, and it’s set to
Group3 in the example). Because these controls share the same group name, and because they have a group name set, you can select multiple RadioButton controls within a single container.

In contrast, the right-hand Border control contains multiple CheckBox controls. Nothing keeps you from selecting as many CheckBox controls as you like. That’s the point: When you use RadioButton controls, you intend for users to select only one per group. When you use CheckBox controls, you intend users to select multiple items.

NOTE This sample uses a StackPanel control as the container for the
groups of RadioButton controls. You may also want to investigate
the GroupBox control, which not only groups the controls but
also adds a caption describing the group. The GroupBox control is,
effectively a simple container with a caption added.

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.