DCSIMG
November 2011 - Posts - Pavel's Blog
Sign in | Join | Help

Pavel's Blog

Pavel is a software guy that is interested in almost everything
software related... way too much for too little time

November 2011 - Posts

WPF Tip: SharedSizeGroup

Published at Nov 26 2011, 05:54 PM by pavely

Suppose we want to display a set of objects in a ListBox (or other ItemsControl) using a DataTemplate that uses a Grid:

  1. <ListBox HorizontalContentAlignment="Stretch" x:Name="_list" >
  2.     <ListBox.ItemTemplate>
  3.         <DataTemplate>
  4.             <Border Margin="2" BorderBrush="Blue" BorderThickness="2" Padding="2">
  5.                 <Grid>
  6.                     <Grid.ColumnDefinitions>
  7.                         <ColumnDefinition Width="Auto" />
  8.                         <ColumnDefinition />
  9.                     </Grid.ColumnDefinitions>
  10.                     <TextBlock Text="{Binding Name}" FontSize="20" Margin="4" FontWeight="Bold" VerticalAlignment="Center"/>
  11.                     <TextBlock Text="{Binding Author}" FontSize="16" Grid.Column="1" VerticalAlignment="Center" Margin="10,4,4,4" />
  12.                 </Grid>
  13.             </Border>
  14.         </DataTemplate>
  15.     </ListBox.ItemTemplate>
  16. </ListBox>

Running this sample with some of my favorite books, yields the following:

image

Note the lack of alignment. How can we fix this?

One simple solution is to replace the Width=”Auto” on the ColumnDefinition with a specific width, like so:

  1. <ColumnDefinition Width="200" />

This might work, but that’s very fragile. New book names might be wider, or the font size may change.

We could also try a “star” ratio, but again, this may or may not work correctly.

The Grid provides an elegant solution: a column’s width (or a row’s height) can share its size with other Grid objects under some parent container. That’s what’s required here, because the DataTemplate causes multiple Grid objects to be created at runtime under the ListBox. If we could somehow connect their columns’ width – we could get the desired effect.

The DefinitionBase class (base of ColumnDefinition and RowDefinition) provides a SharedSizeGroup property – a string that identifies columns/rows that should have the same size if they share the same value of SharedSizeGroup. In our case, the Grids are created by applying the DataTemplate, so the group name need only be specified once, and its exact value is unimportant:

  1. <ColumnDefinition Width="Auto" SharedSizeGroup="abc"/>

To make this work, there’s one more step: set the attached Grid.IsSharedSizeScope to true on some parent of those Grids, to set a context from which the strings may be distinguished. In our case, the ListBox control will do just fine:

  1. <ListBox HorizontalContentAlignment="Stretch" x:Name="_list" Grid.IsSharedSizeScope="True">

And that’s it.

image

Note that (unfortunately) Silverlight does not support this capability.

My C# 5.0 Async Session at Microsoft

Published at Nov 22 2011, 03:36 PM by pavely

Today I presented a session on C# 5.0 asynchronous programming. Thank you all for coming, it was a pleasure to present this interesting stuff. The presentation and demos are attached to this post. The NetFlix and RSS aggregator demos are part of the Async CTP installation.

Any questions or comments are very welcome!

Mandelbrot Set with C# 5.0 Async support

Published at Nov 21 2011, 07:09 PM by pavely

I’ve always been fascinated by the Mandelbrot set. It’s an intriguing set, and the fractals created are truly mind boggling. (for more information on the Mandelbrot, and other such sets, you can start with the above Wikipedia link).

As part of my preparation for tomorrow’s session on C# 5.0 asynchronous programming, I’ve decided to create a WPF application to view and explore the Mandelbrot set, while taking advantage of those new asynchronous features. This turned out to be rather fun. Here’s the initial output:

image

Now you can mark a rectangular are to zoom in:

image

and the result:

image

And a few more zoomed in images:

imageimageimage

This is simply fun!

Note that to compile the code you would need Visual Studio 2010 with SP1 and the latest Async CTP (3) installed.

As an aside, the calculations use the relatively new Complex type introduced in .NET 4.

Happy zooming, and (maybe) see you tomorrow!

The Future of Silverlight

Published at Nov 14 2011, 06:33 PM by pavely

There’s been a lot of talk lately about Silverlight. Will Silverlight 5 (to be released this month) be the last major version of Silverlight? I don’t know, but here are my thoughts on the subject.

Silverlight will stay in the context of Windows Phone, that’s seems pretty sure, but what about the web at large? Is Silverlight (and its rival, Adobe Flash) doomed?

In recent years, there has been gravitation towards standards – that is, world wide standards, not Microsoft’s or anyone else’s for that matter. This is not a bad thing onto itself. But are the standards we’re aiming for the right ones?

HTML has gone a long way since its inception, but its goal and its looks didn’t really change – it’s used to render documents. It was never suited for applications. The web community has done its best, with great feats and toolkits, featuring the infamous JavaScript (officially called ECMAScript) to be the go to guy where dynamic and asynchronous web are concerned.

HTML is “standard”, but even with HTML 4.x, which is with us for quite some time now, not all browsers behave the same – hence the need for various JavaScript libraries (Microsoft’s AJAX library, JQuery and many others) that attempt to hide browser differences (granted – they do a lot more than just that, more in a bit).

HTML 5 is hailed by some as the greatest thing since XML, but hey – it’s still HTML. And JavaScript just wouldn’t go away.

Are the features of HTML 5 comparable to Silverlight (in whatever version)? Probably not. But let’s suppose that they are. Usually, when Silverlight is mentioned, its graphics, animation and video capabilities are discussed and praised, and they are certainly impressive. Does HTML 5 cover all those? Let’s suppose that it does. Does this mean Silverlight (and Flash) are obsolete?

I think not. I think clinging to the current web browser client model is a mistake – that would eventually be rectified, but with the advent of HTML 5, it’s probably going to be a while.

The JavaScript language is not well suited to its purpose, which wasn’t that grandiose when it was first invented by Netscape. It’s a typeless language, with many weirdness inside. With current static languages, such as C#, it’s simply amazing how far away JS really is. I like to work with Silverlight at least because it uses C#, a productive, statically typed (for the most part…) language and has great tool support. When you add to that the application model of Silverlight, its Data Binding capabilities (which have nothing to do with graphics) – it’s hard not to see its appeal. Graphics and animation are one thing; tools for writing actual applications (not documents) are the thing that sets Silverlight apart.

Eventually, I believe the web community will come up with a new standard for web applications. One that would embrace a statically typed, compiled language. It doesn’t have to be C#, and the overall framework does not have to be Silverlight. But it has to be along those lines.

I don’t know what would happen to Silverlight. I’m a little worried for WPF, for that matter, in the Windows world. But one thing is for sure: Silverlight is making a difference just by its very existence.

My two cents.