- Home > Archive: December, 2013
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.
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.
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’.
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.
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.
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.