Nerdy tidbits from my life as a software engineer

Thursday, January 24, 2008

Checkbox Orientation

Here's a strange problem that you think would be easy to solve: how do you get the check box in a CheckBox control to show up on the right-hand side of a check box instead of the left-hand side? None of the standard CheckBox properties allow you to set this, which I find strange (especially considering this is easy to do in winforms). You would think that this should be something that could be accomplished without using control templates, but apparently not.

I did find one easy, although admittedly odd solution to this problem, however: change the FlowDirection from the default (LeftToRight) to RightToLeft! This has the effect of moving the check box to the right-hand side of the text without needing control templates. There is one caveat, however: although the text itself is not reversed, other things, such as semicolons, are. For instance:

<CheckBox Margin="2"
 VerticalAlignment="Center">Auto Delete On Close:</CheckBox>

Will produce a check box with the text on the left and the check box on the right - but the semicolon will be the first character of the string! In other words, this text will actual display, ":Auto Delete On Close" instead of "Auto Delete On Close:", which is what we are trying to do in this situation. The solution is hokey, but it works: move the semicolon to the other side of the string:

<CheckBox Margin="2"
 VerticalAlignment="Center">:Auto Delete On Close</CheckBox>

This will produce your check box with the box on the right and the semicolon on the right, without subclassing, which is what we want. Seems like a lot of work to do something pretty simple, don't you think?

Wednesday, January 16, 2008

Fun With .NET 3.5

I'm having a good time with LINQ and the .NET 3.5 framework in Visual Studio 2008. It's funny how natural LINQ queries are becoming. Where as before, to do things like join two lists and sort by a property, you would need to write all sorts of code with temporary data stores, etc., now this code is reduced to a nice, eloquent, SQL-like blob of code. On top of that, it's also compiled, type-safe, and efficient (or so they say - I would like to learn more about efficiency myself). It's also a wacky technology to get a hold of: to really understand how to write a query, you really do need to understand how it turns from something like:

var query = from i in new int[] {1, 2, 3, 4}
from j in new int[] {5, 6, 7, 8}
let sum = i + j
orderby sum
select sum

Turns into executable code. I am honestly still not sure of how this works. Crazy stuff though, isn't it? Think of the time and effort this technology saves! It really is quite amazing how easy and natural it is to use LINQ in your code. At first, I figured I would use it more to try it out, but I quickly realized that I could use it almost everywhere and that it made my life easier everywhere I went. Now I'm writing LINQ queries everywhere that I can. I need to better understand how 'comprehension syntax' fits works, and how it can be extended. Also, joins are still slightly mysterious to me (I know that they work but I'm not entirely sure why!). And although I'm still slightly perturbed by the use of the var keyword and anonymous types, I have decided that they are very helpful and make certain actions substantially easier as well. It's nice to be getting my feet wet with all of these new technologies. I wonder how long it's going to take before people are using all of this in everything they do. Even the WPF still hasn't taken off yet. How long do we have to wait before people understand the power of the new frameworks?