XAML Browser Applications XBAPs

While we're talking about Visual Studio tools for WPF, you may notice that a few icons away from the Windows Application (WPF) project template is another one called XAML Browser Application (WPF), as shown in Figure 1-8. Figure 1-8. The WPF XAML Browser Application project template in VS05 Figure 1-8. The WPF XAML Browser Application project template in VS05 WPF itself was created as a unified presentation framework, meant to enable building Windows applications with the best features from...

Consistency Across Multiple Grids

Although the row and column spanning features described in the preceding section often make it possible to arrange your UI as you need, it will not always be possible to put all of the information you wish to present into a single Grid element. For example, consider a scrollable Grid with headings.* You could just put headings and contents into a single Grid and then place that Grid in a ScrollViewer to make it scrollable, but this suffers from a problem, which Example 3-15 illustrates. <...

The User Experience

The user experience for running a ClickOnce locally installed application begins with a web page, such as the one shown in Figure 2-9, that includes a link to install the ClickOnce application. Clicking the link for the first time shows a download progress dialog similar to Figure 2-10. Verifying application requirements. This may take a few moments. Figure 2-10. Progress dialog for checking the application manifest Once the metadata file describing the application deployment settings has been...

Rendering On Demand

The key to custom on-demand rendering is the OnRender method. WPF calls this method when it needs your component to generate its appearance. (This is how the built-in shape classes render themselves.) The virtual OnRender method is defined by the UIElement class. Most elements derive from this indirectly via FrameworkElement, which adds core features such as layout and data binding. Example 13-59 shows a custom element that overrides OnRender. Example 13-59. A custom OnRender implementation...

Binding to Relational Data

Although UI designer support is still being developed to help bring relational data into your WPF pages specifically, the tools we've already got can be pressed into service for WPF work without issue. For example, assume a table like the one in Figure 7-18 defined in an Access database (family.mdb). Figure 7-18. A Person table in Access (family.mdb) Although we could write the ADO.NET code to bring this table into our project, we don't have to. Instead, we can bring in the data using the typed...

Object Data Provider

WPF ships with two data source providers, both derived from the DataSourceProvider base class ObjectDataProvider and XmlDataProvider. Data source providers create a layer of indirection for any kind of operation that produces objects against which to data-bind. For example, if we wanted to load a set of Person objects over the Web, we could encapsulate that logic into a bit of code, as shown in Example 7-29. Example 7-29. A type to be used by ObjectDataProvider public class Person...

Master Detail Binding

We've seen binding to a single object. We've seen binding to a single list of objects. Another very popular thing to do is to bind to more than one list, especially related lists. For example, if you're showing your users a list of customers and then, when they select one, you'd like to show that customer's related orders, you'll want master-detail binding. Master-detail binding is a form of filtering, where the selection in the master list (e.g., customer 452) sets the filtering parameters for...

[i1 Tic TacToe

Tic-tac-toe boards don't have rounded insets What we really want here is to be able to keep the behavior i.e., holding content and firing click events , but to take over the look of it. WPF allows this kind of thing because the intrinsic controls are built to be lookless i.e., they provide behavior, but the control's user can swap out the look completely . The default look comes from the system-provided template, as described in Chapter 12. Remember from Chapters 6 and 8 how we used...

Dock Panel

DockPanel is useful for describing the overall layout of a simple user interface. You can carve up the basic structure of your window using a DockPanel, and then use the other panels to manage the details. A DockPanel arranges each child element so that it fills a particular edge of the panel. If multiple children are docked to the same edge, they simply stack up against that edge in order. By default, the final child fills any remaining space not occupied by controls docked to the panel's...

Commands

Commands with a menu and text box continued lt MenuItem Header Cu_t gt lt MenuItem Header _Copy gt lt MenuItem Header _Paste gt lt ToolBarTray DockPanel.Dock Top gt lt ToolBar gt lt Button Command Cut Content Cut gt lt Button Command Copy Content Copy gt lt Button Command Paste Content Paste gt lt ToolBar gt lt ToolBarTray gt lt TextBox gt lt DockPanel gt Each menu item is associated with a command. This is all that's required to invoke these clipboard operations on the text box...

Window Location and Size

You can manage the x and y locations of a window with the Top and Left properties, whereas you can influence the Z order with the TopMost property. Across the entire desktop, all windows with the TopMost property set to true appear above all of the windows with the TopMost property set to false, although the Z order of the windows within their layer is determined by user interaction. For example, clicking on a non-topmost window will bring it to the top of the non-topmost layer of windows, but...

Building Localizable Applications with XAML

Because XAML is compiled into BAML resources that are retrieved using a ResourceManager, localizability is an intrinsic feature of any WPF application built using XAML. If a satellite resource assembly for the current culture is present, and it contains a localized BAML resource, WPF will use that instead of the one in the main assembly. However, there is no built-in support for localizing WPF applications in Visual Studio 2005, so a few manual steps are involved. The localization process will...

Data Driven UI

Templates enable a certain kind of UI programming sometimes called data-centric UI and sometimes called data-driven UI. The idea is that the data is the most important thing in our application, and through the use of declarative UI techniques as enabled by XAML we shape the data into something suitable for presentation to the user. Ideally, we do this without changing the underlying data at all, but by instead transforming the data on its way to the user as appropriate. For example, getting...

Binary Resources

Although ResourceDictionary and the resource scope system are fine for data that can easily be contained in an object, not all resources fit comfortably into this model. Often we need to deal with binary streams. For example, images, audio, and video have efficient binary representations, but they are not particularly at home in markup, and in the world of objects they are usually represented by wrappers for the underlying data. Markup itself also presents a challenge XAML pages must somehow...

Bitmap Effects

All user interface elements have a BitmapEffects property. You can use it to apply a visual effect to the element and all of its children. All of these effects use bitmap processing algorithms, hence the name. Example 13-37 applies a BlurBitmapEffect to one of its StackPanel elements. lt StackPanel Orientation Horizontal gt lt StackPanel Orientation Vertical gt lt TextBlock Text Abcdef TextAlignment Center FontWeight Bold gt lt RadioButton Content Better in position 1 GroupName r gt lt...

Is aucci

TextElement properties applied to descendents of a button Table 14-1 lists the inherited attached properties defined by TextElement. Recall that the default template for a Button includes a ContentPresenter to host the content. If the content is plain text, a ContentPresenter will generate a TextBlock to display that text. This TextBlock inherits the two TextElement properties set on the Button. Table 14-1. TextElement inherited properties Typeface family e.g., Palatino Linotype,...

Routed Events

The .NET Framework defines a standard mechanism for managing events. A class may expose several events, and each event may have any number of subscribers. WPF augments this standard mechanism to overcome a limitation if a normal .NET event has no registered handlers, it is effectively ignored. Consider what this would mean for a typical WPF control. Most controls are made up of multiple visual components. For example, suppose you give a button a very plain appearance consisting of a single...

Flow Direction

The FlowDirection property controls how text flows the default is based on the system locale. For example, in English-speaking locales, it will be left to right, but many cultures use the alternative right-to-left style. Setting the FlowDirection property to RightToLeft affects the flow direction of all text, and of any WrapPanel elements contained within that element. This is an inherited property, meaning that it applies to all its descendants setting this on a window implicitly sets it for...

Logical and Visual Trees

The existence of templates leads to an API design dilemma that the WPF architects had to resolve. If a developer wishes to access the elements in the UI, should she see the fully expanded tree, containing all the instantiated templates Although this would put the developer in full control, it might be rather cumbersome often a developer only really cares that there is a Button present, not about the structure of its appearance. On the other hand, to present the simple pre-expansion view would...

Wrap Panel

WrapPanel works just like a StackPanel until it runs out of space. If you provide a horizontal WrapPanel with more children than will fit in the available width, it will arrange its content in a way similar to how a word processor lays out words on a line. It puts the children in a row from left to right until it runs out of space, at which point it starts on the next line. WrapPanel is very simple to use. Just as with a StackPanel, you add a sequence of children, as Example 3-3 shows. lt...

How This Book Is Organized

Here's what each chapter of this book will cover This chapter introduces the basics of WPF. It then provides a whirlwind tour of the features that we will cover in the following chapters, so you can see how everything fits together before we delve into the details. Chapter 2, Applications and Settings In this chapter, we show how WPF manages application-wide concerns, such as the lifetime of your process, keeping track of open windows, and storing application-wide states and settings. We also...

Shortcut

Flowdocumentpageviewer

However, if the only reason you're using Label is to show an access key underline, there's a better alternative. When you provide Label or any control that supports the content model with a string containing an underscore, it generates an AccessText element to present that string instead of a TextBlock. So, it would be more efficient to use this directly, as Example 14-14 does. Example 14-14. Access key underline with AccessText lt AccessText Text S hortcut gt The rule is simple use the Label...

Image Brush Drawing Brush and Visual Brush

Visual Studio Imagebrush

The ability to fill shapes with a pattern or image of some kind is often useful. WPF provides three brushes that allow us to paint shapes with whatever graphics we choose. The ImageBrush lets us paint with a bitmap. With DrawingBrush, we use a scalable drawing. VisualBrush allows us to use any UI element as the brush image we can in effect use one piece of our user interface to paint another. All of these brushes have a certain amount in common, so they all derive from the same base class,...

XML Data Source Provider

In addition to object and relational data, WPF also supports binding to XML data. For instance, Example 7-39 shows some family data represented in XML. Example 7-39. A random family rendered in XML lt Family lt Person Name Tom Age 11 gt lt Person Name John Age 12 gt lt Person Name Melissa Age 38 gt lt Family gt With this file available in the same folder as the executing application, we can bind to it using the XmlDataProvider, as shown in Example 7-40. Example 7-40. An XmlDataProvider in...

Stack Panel

StackPanel is a very simple panel that arranges its children in a row or a column. You will not normally use StackPanel to lay out your whole user interface. It is most useful for arranging small subsections. Example 3-1 shows how to build a simple search user interface. Example 3-1. StackPanel search layout lt StackPanel Background ECE9D8 gt lt TextBlock Margin 3 gt Look for lt TextBlock gt lt ComboBox Margin 3 gt lt TextBlock Margin 3 gt Filtered by lt TextBlock gt lt ComboBox Margin 3 gt lt...

Path

Path is by far the most powerful shape. All of the shapes we have looked at up to now have been supplied for convenience, because it is possible to draw all of them with a Path. Path also makes it possible to draw considerably more complex shapes than is possible with the previous shapes we have seen. As mentioned earlier, the various classes derived from Shape are essentially high-level wrappers around underlying geometry objects. Path is explicit about this its shape is defined by its Data...

Default Collection Views

The SDK documentation for the individual views will tell you how each different kind of collection data is mapped to a default view, but Table 7-1 is a handy guide to help you along. Table 7-1. The default views for each collection data type Table 7-1. The default views for each collection data type Hopefully, you'll test the rest of your code before shipping it, too, but it never hurts to point these things out If you don't like the view that WPF provides, you can create your own...

Scrollable Region and IScroll Info

If you place a panel or any other ordinary element inside a ScrollViewer, the ScrollViewer will measure its size in the normal way the scrollable area essentially sizes to content unless the available area is surplus to requirements, in which case the ScrollViewer gives the child all of the available space . It keeps track of the currently visible region, and moves the child content around as required. Most of the time, this is exactly the behavior you require. However, occasionally you might...

Simple Publishing

For anyone to use this wonderful application, it must be published. The simplest way to publish your WPF application is by right-clicking on the project in the Solution Explorer and choosing the Publish option, which will bring up the first page of the Publish Wizard shown in Figure 2-5 . Figure 2-5. Publish Wizard publish location Figure 2-5. Publish Wizard publish location Figure 2-5 asks you to choose where you'd like to deploy your application, including to the disk, to a network share, to...

XBAP Publication and Deployment

The publication of an XBAP happens exactly like the publication of a WPF application via ClickOnce, as discussed in the Chapter 2. I'll wait here while you refresh your memory. Right-clicking on your XBAP project and choosing Publish brings up the Publish Wizard, which leads you through the publication process. Unlike the Publish Wizard for standalone ClickOnce applications, this time you won't get a publish.htm, but here's a template to get you started lt head gt lt title gt Welcome to XBAP...

Data validation

Just because the user clicks the OK button doesn't mean that everything's OK the data the user entered generally needs validation. You'll recall from Chapter 6 that WPF provides per-control validation as part of the binding engine. Dialogs, an example of which is shown in Example 10-18, are an excellent place to apply this technique. Example 10-18. Data validation and dialogs lt -- 1st row report folder setting -- gt lt Label gt Report _Folder lt Label gt lt TextBox Name reportFolderTextBox...

Creating Bitmaps

Wpf Net Image Text Rendertargetbitmap

RenderTargetBitmap lets you create a new a bitmap from any visual. Example 13-32 renders a red ellipse into a bitmap. Example 13-32. Using RenderTargetBitmap RenderTargetBitmap bmp new RenderTargetBitmap 300, 150, Dimensions in physical pixels 300, 300, Pixel resolution dpi PixelFormats.Pbgra32 Ellipse e new Ellipse e.Fill Brushes.Red e.Measure new Size 96, 48 e.Arrange new Rect 0, 0, 96, 48 You can choose any resolution you like for the output in this case, we're creating a 300 dpi bitmap...

Render Transform and Layout Transform

You can use both the RenderTransform and LayoutTransform properties to apply a transform, such as scaling or rotation, to an element and all of its children. Transforms are described in Chapter 13, but it is useful to understand their impact on layout. If you apply a transform that doubles the size of an element, the element will appear to be twice as large on-screen. You would normally want the layout system to take this into account if a Rectangle with a Width of 100 is scaled up to twice its...

WPF from Scratch

Example 1-1 is pretty much the smallest WPF application you can write in C . Example 1-1. Minimal C WPF application using System.Windows the root WPF namespace namespace MyFirstWpfApp class MyApp STAThread static void Main the WPF message box MessageBox.Show Hello, WPF The STAThread attribute signals .NET to make sure that when COM is initialized on the application's main thread, it's initialized to be compatible with single-threaded UI work, as required by WPF applications. In fact, this is...

Preface

It's been a long road to the Windows Presentation Foundation. I learned to program Windows from Programming Windows 3.1, by Charles Petzold Microsoft Press . In those days, programming for Windows was about windows, menus, dialogs, and child controls. To make it all work, we had WndProcs window procedure functions and messages. We dealt with the keyboard and the mouse. If we got fancy, we would do some nonclient work. Oh, and there was the stuff in the big blank space in the middle that I could...