Grasp WPF with key points

 When the Windows Presentation Foundation (WPF) first appeared , most of the articles and demo applications touted its superb rendering engine and 3D capabilities. That does not demonstrate the powerful real-world functionality of WPF. WPF offers excellent support for managing the display and editing of complex data. In fact, most of us earn a living by creating software to display and edit large amounts of complex business or scientific data. This is also my start point of this anticle. :-)

Developing the user interface of a professional software application is not easy. It's not simply piling up UI controls.  It can be a murky blend of data, interaction design, visual design, connectivity, multithreading, security, internationalization, validation, unit testing, and a touch of voodoo. Considering that a user interface exposes the underlying system and must satisfy the unpredictable stylistic requirements of its users, it can be the most volatile area of many applications.

WPF is very powful. User interfaces and resources can be declared using Extensible Application Markup Language (XAML), an XML-based markup language. Anything that is possible to do in XAML is also possible to achieve in code. If you want to rapidly create one application with vivid user interface, Trust me, WPFis a good choice.

Before diving into WPF hand manual, the below items can tell you the key points of WPF learning.

UI layout

Give a good arrangement  to the layout of your UI elements. You'd better to make your window is resize-able. At the same time, thinking about globalization and Localization, you should adopt flow famat to make sure your controls have enough room to hold content inside. Of cause, the good UI layout should reflect the Hierarchical Data behind.

Template

If you have ever writen MFC code, you will undertand why i love it so much.

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.FrameworkTemplate
      System.Windows..::.DataTemplate
      System.Windows.Controls..::.ControlTemplate
      System.Windows.Controls..::.ItemsPanelTemplate

In WPF, the ControlTemplate of a control defines the appearance of the control. You can change the structure and appearance of a control by defining a new ControlTemplate for the control. In many cases, this gives you enough flexibility so that you don't have to write your own custom controls.

In WPF, you use a DataTemplate to define the visual representation of data.

Data binding

I can never emphasis data binding too much. Please check your WPF menual book.

Routed Events and Commands

Event Routing

Understanding a little about the logical and visual trees is important because routed events get routed based primarily on the visual tree. Routed events support a RoutingStrategy of Bubble, Tunnel, or Direct.

Bubble is the most common and means that an event will bubble (propagate) up the visual tree from the source element until either it has been handled or it reaches the root element. This allows you to handle an event on an object further up the element hierarchy from the source element. For example, you could attach a Button.Click handler on the enclosing Grid element instead of directly on the button itself. Bubble events just have names that indicate their action (for example, MouseDown).

Tunnel events go in the other direction, starting at the root element and traversing down the element tree until they are handled or reach the source element for the event. This allows upstream elements to intercept the event and handle it before the event reaches the source element. Tunnel events have their names prefixed with Preview by convention (such as PreviewMouseDown).

Direct events behave like normal events in the .NET Framework. The only potential handler for the event is a delegate that is hooked up to the event.

Usually if a Tunnel event is defined for a particular event, there is a corresponding Bubble event. In that case, the Tunnel event fires first, starting at the root and working its way down to the source element looking for a handler. Once it has been handled or has reached the source element, the Bubble event is fired, working its way up from the source element and looking for a handler. A Bubble or Tunnel event does not stop its routing just because an event handler is called. If you want to stop the bubbling or tunneling process, you mark the event as handled in your event handler

 

The implications are quite powerful. For example, if I choose to replace the default button appearance by applying a control template that contains an Ellipse element, I don't have to do anything to ensure that clicks outside the Ellipse don't fire the Click event. Clicks just outside the edge of the Ellipse will still be inside the rectangular bounds of my button, but Ellipse has its own hit detection for MouseLeftButtonDown, and the empty portions of the button outside the Ellipse do not. 

Routed Commands Overview

Now that you have seen routed events, you are ready to understand routed commands. WPF-routed commands give you a specific mechanism for hooking up UI controls such as toolbar buttons and menu items to handlers without introducing a lot of tight coupling and repetitive code into your application. Routed commands give you three main things on top of normal event handling:

·         Routed command source elements (invokers) can be decoupled from command targets (handlers)—they do not need direct references to one another, as they would if they were linked by an event handler.

·         Routed commands will automatically enable or disable all associated UI controls when the handler indicates the command is disabled.

·         Routed commands allow you to associate keyboard shortcuts and other forms of input gestures (ink gestures, for example) as another way to invoke the command.

In addition, a specific flavor of routed command, the Routed­UICommand class, adds the ability to define a single Text property to be used for the text prompting of any controls that are invokers for the command. The Text property can also be localized more easily than visiting each associated invoker control.

Another point that I want to mention is:

Working with Collection Views

When WPF controls bind to a collection of data, they do not bind directly to the collection itself. Instead, they implicitly bind to a view automatically wrapped around that collection. The view implements the ICollectionView interface and can be one of several concrete implementations, such as ListCollectionView.

A collection view has several responsibilities. It keeps track of the current item in the collection, which usually translates to the active/selected item in a list control. Collection views also supply a generic means of sorting, filtering, and grouping items in a list. Multiple controls can bind to the same view around a collection so they are all coordinated with each other.

 

Enjoy the developing with WPF. If you have know the magic power of WPF, I am sure you will like it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值