Off the Ridge Code Newsletter


Monthly Archives: December 2013

WPF – Part: 2

Published December 4, 2013 in WPF - 0Comments

WPF overview tutorial Part: 2 – Part 1 is here.

Let’s take a look at WPF from 10,000 foot view:

Direct X – used to render the actual pixels onto the screen.

Composition Engine – (Media Integration Layer) – It’s a native component written in unmanaged code that resides in milcore.dll and is responsible for providing support 2D and 3D imaging. It interfaces directly with Direct X. It’s written in unmanaged code for a simple reason: Direct X uses COM Interfaces to communicate with the outside world and these calls do not come very cheap in the managed world of interop calls.

Presentation Core – exposes rendering services of the Composition Engine – essentially managed wrapper for the Composition Engine.

Presentation Framework – provides concepts such as: Control, Layout, command handling, data binding, etc.

XAML or Extensible application markup language.

I’ve said it again, and I’ll say it again: WPF doesn’t need XML and XML doesn’t need WPF. It’s simply a language for building .NET objects and XAML user interfaces are built as trees of objects.

Let’s take a look at a simple button:

[[code style="color: black; word-wrap: normal;"]]czo1NTk6XCIxOiAgJmx0O1BhZ2UgIDxiciAvPjI6ICAgeG1sbnM9XCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dpbmZ4LzIwe1smKiZdfTA2L3hhbWwvcHJlc2VudGF0aW9uXCIgIDxiciAvPjM6ICAgeG1sbnM6eD1cImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd2luZntbJiomXX14LzIwMDYveGFtbFwiJmd0OyAgPGJyIC8+NDogICAmbHQ7R3JpZCBIb3Jpem9udGFsQWxpZ25tZW50PVwiQ2VudGVyXCIgVmVydGljYWxBe1smKiZdfWxpZ25tZW50PVwiQ2VudGVyXCImZ3Q7ICAgPGJyIC8+NTogICAgICZsdDtCdXR0b24geDpOYW1lPVwiWEFNTEJ1dHRvblRlc3RcIiAgIDxie1smKiZdfXIgLz42OiAgICAgICAgICAgICAgICAgICAgIEZvbnRGYW1pbHk9XCJWZXJhbmRhXCIgIDxiciAvPjc6ICAgICAgICAgICAgICAgICAgIHtbJiomXX0gIEZvbnRTaXplPVwiNDJcIiAgPGJyIC8+ODogICAgICAgICAgICAgICAgICAgICBGb3JlZ3JvdW5kPVwiRGFya1JlZFwiJmd0OyAgPGJyIHtbJiomXX0vPjk6ICAgICAgIF9TdWJtaXQhICA8YnIgLz4xMDogICAmbHQ7L0J1dHRvbiZndDsgICA8YnIgLz4xMTogICAmbHQ7L0dyaWQmZ3Q7e1smKiZdfSAgPGJyIC8+MTI6ICAmbHQ7L1BhZ2UmZ3Q7ICA8YnIgLz5cIjt7WyYqJl19[[/code]]
Note: On Line 4 HorizontalAlignment=”Center” VerticalAlignment=”Center” properties describe how a child elements should be position within its parent. i.e. this is what actually makes a button, otherwise it would fill out all available space.
Also, note that I have prefixed the ‘_Submit’ – this is how you define Mnemonic in WPF. (you need to hold an ALT key in order for it to show up)

XAML is a language for building .NET objects and here is how we would accomplish the same thing in C# using object initializer:

[[code style="color: black; word-wrap: normal;"]]czoyNDM6XCIgIHZhciB4bWxCdXR0b25UZXN0ID0gbmV3IEJ1dHRvbiAgPGJyIC8+ICAgICAgIHsgIDxiciAvPiAgICAgICAgIEZvbnR7WyYqJl19RmFtaWx5ID0gbmV3IEZvbnRGYW1pbHkoXCJWZXJhbmFkYVwiKSwgIDxiciAvPiAgICAgICAgIEZvbnRTaXplID0gNDIsICA8YnIgLz4ge1smKiZdfSAgICAgICAgRm9yZWdyb3VuZCA9IEJydXNoZXMuRGFya1JlZCwgIDxiciAvPiAgICAgICAgIENvbnRlbnQgPSBcIl9TdWJtaXRcIiAgPHtbJiomXX1iciAvPiAgICAgICB9OyAgPGJyIC8+XCI7e1smKiZdfQ==[[/code]]

There are two concepts how the input is handled in WPF, these are routed events and routed commands.
Events routing allows events to be handled by ancestor of the element who originated the event – there are two patterns for doing that:
1. Bubbling  (most common)
    The event propagates up the visual tree from the source element.

2. Tunneling
    These, as you may suspect, go the other way, down from the source element.

Events come in pairs: There’s usually a preview event that tunnels, followed by an event that bubbles – the reason being is so that the main element gets the first opportunity to respond or react to that event.

WPF controls have no intrinsic look, they are ‘lookless’ – this is because templates can replace default look with your own design, unlike in WIN32 where we do have a default look.

Let’s take a look at how to define a template for a button, i.e. custom look, using property element syntax:

Line 2: The part before the dot is interpreted as the ‘class name’ and the part after the dot as the ‘property name’.
Line 3: TargetType=”Button” – defines what control is the template for.

[[code style="color: black; word-wrap: normal;"]]czoyNzA6XCIxOiAgJmx0O0J1dHRvbiBXaWR0aD1cIjY0XCIgSGVpZ2h0PVwiMzBcIiZndDsgIDxiciAvPjI6ICAgICAgICAmbHQ7QnV0dG9ue1smKiZdfS5UZW1wbGF0ZSZndDsgIDxiciAvPjM6ICAgICAgICAgICZsdDtDb250cm9sVGVtcGxhdGUgVGFyZ2V0VHlwZT1cIkJ1dHRvblwiJmd0O3tbJiomXX0gIDxiciAvPjQ6ICAgICAgICAgICZsdDsvQ29udHJvbFRlbXBsYXRlJmd0OyAgPGJyIC8+NTogICAgICAgICZsdDsvQnV0dG9uLlRle1smKiZdfW1wbGF0ZSZndDsgIDxiciAvPjY6ICAgICAgJmx0Oy9CdXR0b24mZ3Q7ICA8YnIgLz5cIjt7WyYqJl19[[/code]]
The code above represents the natural look for the button which essentially is ‘no look’.
Let’s fill in the appearance: (fully working XAML)
[[code style="color: black; word-wrap: normal;"]]czoxMzEzOlwiMTogICZsdDtXaW5kb3cgeDpDbGFzcz1cIldwZkFwcGxpY2F0aW9uMi5NYWluV2luZG93XCIgIDxiciAvPjI6ICAgICAgeHtbJiomXX1tbG5zPVwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93aW5meC8yMDA2L3hhbWwvcHJlc2VudGF0aW9uXCIgIDxiciAvPjM6ICB7WyYqJl19ICAgIHhtbG5zOng9XCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dpbmZ4LzIwMDYveGFtbFwiICA8YnIgLz40OiAgICAgIFRpe1smKiZdfXRsZT1cIk1haW5XaW5kb3dcIiBIZWlnaHQ9XCIzNTBcIiBXaWR0aD1cIjUyNVwiJmd0OyAgPGJyIC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ICAgICAgICAgICBIb3Jpem9udGFsQWxpZ25tZW50PVwie1RlbXBsYXRlQmluZGluZyBIb3Jpem9udGFsQWxpZ25te1smKiZdfWVudH1cIiAvJmd0OyAgPGJyIC8+MTU6ICAgICAgICAgICAgJmx0Oy9HcmlkJmd0OyAgPGJyIC8+MTY6ICAgICAgICAgICZsdDsvQ29ue1smKiZdfXRyb2xUZW1wbGF0ZSZndDsgIDxiciAvPjE3OiAgICAgICAgJmx0Oy9CdXR0b24uVGVtcGxhdGUmZ3Q7ICA8YnIgLz4xODogICAgICB7WyYqJl19ICBfU3VibWl0ICA8YnIgLz4xOTogICAgICAmbHQ7L0J1dHRvbiZndDsgIDxiciAvPjIwOiAgICAmbHQ7L0dyaWQmZ3Q7ICA8YnIgL3tbJiomXX0+MjE6ICAmbHQ7L1dpbmRvdyZndDsgIDxiciAvPlwiO3tbJiomXX0=[[/code]]
Line: 10 “TemplateBinding Background” – allows us to bind the property ‘Background’ on line: 6 to the Content Presenter which essentially is a placeholder for any XAML content to be used during runtime.

The curly brackets tell XAML that we are using something called: the markup extension. (more about it in future posts). For now, let’s just say that markup extensions decide during runtime how the properties are set. What line 10 says is that we are creating an object of type: TemplateBinding and we are passing a string (yes, string) called ‘Background’ to a constructor.

Primitive elements:

The elements that define or rather have template are the controls in WPF, and as you may suspect not everything is a control in WPF. In other words you don’t have to worry as to how anything ever appears on your screen when programming in WPF. It’s only the elements that derive from the Control class, such as button that look for their appearance in template.
For example Rectangle, TextBlock and Image derive from the framework element – these are primitives and do not have a template of their own.

Layout primitives:

Grid Panel – flexible grid-based area with columns and rows.
Canvas Panel – fixed area to position elements by the use of coordinates.
Stack Panel – simple vertical or horizontal arrangement in a single line.
Dock Panel – allows to arrange child elements vertically or horizontally relative to each other; similar to windows forms docking.
Wrap Panel – allows to arrange child elements in a flow-like formatting.