Category Archives: WPF

A in XAML: Ain’t for WPF/Silverlight

Even though XAML is officially the acronym of eXtensible Application Markup Language,- a generic term-, you can not usually separate it from UI frameworks, WPF and Silverlight. This creates some confusion, most people think of WPF as the declaration of UI with XML. Not that this is wrong, this explanation does not necessarily suggest decoupling data, logic and user interaction. XAML only helps WPF to achieve it, there is definitely more in WPF. I admit, XAML originally stood for eXtensible Avalon (code-name for WPF) Markup Language. But hey, there is more in XAML, too.

Since the first day I learned about WPF, I almost never used a UI designer tool. Perhaps, that is why I started using XAML everywhere I could, one could say it was love at first sight. They say love lasts roughly 3 years and even though it has been a little bit longer than that for me, I still feel strong about it. Lucky for me, Microsoft does also. Together with the recent release of VS2010 Beta 2, XAML finally has its own assembly, System.Xaml.dll targeting .NET Framework 4 Beta 2. I have now plenty of projects which can loose their dependencies on PresentationCore.dll, PresentationFramework.dll and WindowsBase.dll. I hope to convert them all easily and start making more.

First one to go was my almost forgotten open-source project Geostructor. I was expecting minor problems but I was wrong. I had none except the fact that, I had unconsciously created dependencies on WPF assemblies. After moving those dependent codes to where they really belong, I was ready to go with one little replacement. Replaced all class references of XamlReader and XamlWriter with the new XamlServices class. Not only everything worked as expected, some of my problems were gone too. This is what made me to write about the problem.

Back when I first encountered this problem, I was planning to post my solution here. Then, I heard about a new feature in the upcoming System.Xaml.dll and thought I should wait for it. In other words, I was lazy. So here it goes:

Old Problem

Here is a sample file saved by Geostructor v0.3, which targets .NET Framework 3.5 SP1:

Here, RelatedPoint property of RelativePoint is of type IGPoint which inherits INotifyPropertyChanged interface. RelatedPoint objects listen to the PropertyChanged events of the referenced objects. In order to keep these references after saving and loading back, I had to prevent XamlWriter from producing such result:

Old Solution

Continue reading

Expander inside ScrollViewers, a.k.a. when WPF power is out of control

Yesterday, I spent almost an hour on a small but annoying problem. I was working on a visual editor for an IDE, I develop for internal-use. Have I mentioned before that I started working (for more than a couple of months now) in a company again? Clearly, I haven’t been blogging actively passed year. Let’s see whether I will be able to change this in 2009 or not.
Anyway, this visual editor is for a DSL, or should I say a XAML grammar? It was the perfect opportunity to show the power of WPF. However;

Power is nothing without control
Power is nothing without control

Problem:

Visual editor’s job is to display the workflow in a tree view fashion. I chose displaying listboxes inside listboxes instead of a treeview. Each last node has an expander inside the listboxitem container. When user expands an item, containing listbox grows to display all of the items. I have no problem with that, in fact, that is exactly what I am grateful for. There is one catch, though. Expander controls not only expand but also shrink. When it does, listbox sits back and enjoys the void. A small sample to play with:

Solution:

You probably did know this. I did also, however when things are under multiple levels of ControlTemplates, DataTemplates and Styles, I was reluctant to go deep and investigate. Actually, I was reluctant to do anything at all. I had a great new years eve party and a greater headache. Luckily, WPF also provides an easy solution. Although, I spent a lot time to guess the problem.

Am I wrong to think that this was the default behavior when a control with a scrollviewer is not bounded? If I am, I can easily blame it on countless shots or hitting the floor so many times. Who else did work Friday, just to see “s?he” could not?

WPFix Part 3 (Extension Methods)

One of the new features, a.k.a. syntactic sugars, in C# 3.0 is extension methods. At first, it looks like compiler magic and it is no big deal. But isn’t it the same about class method? When a function argument is hidden, suddenly whole world changes. Clearly, OOP is not just a hidden this pointer, but we can think of extension methods as class methods that will only access the public fields. This may look like a step backwards but we are not living in an ideal world, in fact not even close to ideal. This is more like a side-step that will provide new ways forward. One of these new ways is LINQ, language integrated query. However, it is hard to come up with another as useful as LINQ. Is there a way to utilize extension methods in WPF as we did lambda expressions?

While introducing the lambda converters, I said that implementing IValueConverter or IMultiValueConverter is not a complete waste of time. There were two reasons behind this statement and hence two problems ahead of lambda converter with dreams of being the ultimate binding converter.

Problem 1

One of them was code maintainability in large projects, where a converter can be used more than once. Typing the same expression again and again is not an option.

Solution 1

Idea occurred to me, while reading Kent Bogaart‘s article on WPF converters and Josh Smith‘s article on piping value converters. They have both suggested a way to reuse and combine converters. Then, I had one of those, wait a minute, moments. This is where extension methods kick in. We could write our Convert method in an extension method and use them inside the expressions. Not only, maintaining these converters will be easier, but also they will be combinable by nature.

Continue reading

WPFix Part 2 (Binding Extension)

This time, I will skip the introduction and jump right into technical stuff.

In the previous post, I tried to reduce XAML’s dependency to code files for even the simplest operations. There were a lot of ways other than my lambda converter. We could write a custom binding which could save a lot more of typing. However, my intention is not to type less, but to keep relevant parts together. Still typing less does not hurt, as long as it is clear. We will come back to this, but before that …

Problem 1

Suppose we have an adjustable background color. Don’t worry, I am not going to write another custom color picker for WPF. I will try to use the lambda converter.

Parsing this lambda expression looks a bit harder. Multiple parameters are not a problem but SolidColorBrush and Color classes are.

Solution 1

Continue reading

WPFix Part 1 (Lambda Converter Extension)

As a mathematician, I always had a fondness for languages. As a programmer, this fondness became a devotion. Some might think that language is just a part of implementation which follows analysis and modeling. I agree that implementation is the least important one. I agree that it is like a tool for implementation, you can choose one or the other. I agree that a separate article, -which does not contain WPF in its header-, is more appropriate for this kind of topic. However, I also believe that programming languages have a much broader effect which includes modeling and even analysis. Our process of thinking is effected by our ways of communication.

Eventually, I will connect this to WPF, somehow in the future. In the last few years, I suppose the number of new programming languages has an exponential growth if we also include the new versions. Some believe that domain specific languages will reproduce asexually, some believe that languages converge to one as they copy their best parts. Even if there is a convergence, there is long way on which we speed up. My guess is that the hypothetical unified programming language, which will be the base of many domain specific languages and will act as a connection between them, will be the way to go. While waiting, I suggest we learn a few of these doomed languages. I hope, we will together witness how less these paths are traveled. I promise that following this post, my technical and theoretical musings will also travel separately. But now, lets start by a few technical ones. C# 3.0 and XAML are two new languages in two different paths. Lets get them together!

I always try to defend WPF in discussions with my colleagues. However, I will try to criticize WPF in this series of articles, namely WPFix. Visual Studio 2008 is to be released by the end of this month but I think, WPF and XAML still have a long way to go. Don’t get me wrong, I am just as glad as you are. I only wish, there was a better way than lengthening the evaluation period to spread the ideas. I will try to keep my critiques precise and search for practical solutions. Hopefully, I will also provide introductory links.

Problem

Data binding is pretty much the heart of WPF. We can think of WPF as a pretty complicated converter which consists of little converters for simple data types. However, these converters in the XAML may appear smaller than they are.

This may not be a complete waste of time from code maintenance perspective. But we, mortals, sometimes need a more practical method.

Solution

A similar problem in C# has been resolved lately by lambdas. You are no longer have to declare your method which won’t be used anywhere else. Here comes another future article name to my mind, maintenance is overrated. Personal Note: Use more controversial article names than WPFix Part 1. Back to our problem, we need a way to express ourselves in XAML. By express, I mean code. Hence lambda expressions.
Continue reading