Here I will post problems I and my colleagues met and solutions we found.

Wednesday, March 04, 2009

Fill WrapPanel from the list (WPF)

Sometime, when it is necessary to make a choice from the list, and the list is not that big, it makes sense to use buttons with some actions instead of lists. I found useful technique to do it when the list of actions is dynamic.

The idea is somehow populate WrapPanel with buttons dynamically. We can do it by replacing template for ItemsController.

I lave a collection of elements (it will be assigned to the ItemsSource property in runtime) with Key and Value properties.



<ItemsControl x:Name="activitiesControl" Margin="10">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{DynamicResource ActionButton}" HorizontalAlignment="Right" Margin="5"
Content="{Binding Value}" Width="200"
Command="{Binding Path=ViewModel.ActionTypeCommand,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=local:CustomerEditView}}"
CommandParameter="{Binding Key}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>


Note that I am using Commands. Finally, we have it. Not in Silverlight yet though.

Sunday, March 01, 2009

Application Architecture Guide 2.0

I am currently reading Application Architecture Guide 2.0 I wouldn't say that this is a revolutionary document, but I enjoy reading. I find it very aligned with the way I see the subject, and as it is often the case, it helps me organize my thoughts by putting everything into it's place.

One of the things I would recommend this paper for is it's not just theory or collection of "design patterns". With all my respect for the latter, many authors just abuse the term and sound very far from real world of day-to-day developers. Here I find some practical advices, key points that should not be missed when creating application architecture and common mistakes. There is also understanding of the real world by acknowledgment that development became "agile". And again, the term is not abused.

The volume is quite big, and there are many repetitions, so I just somewhere in the first quarter of the document. Hopefully the last three quarters will be as good as first one.

Binding to nullable values (WPF)

I don't usually use nullable types, since my approach is to use them only when there is real difference between zero and null values, and it's not often when such difference exists. However, sometimes you may not really know how the value is going to be used. And then having it nullable may be preferable.

That was the case in one of our custom projects. Naturally, you would want to display empty edit box for null value, which was defined as nullable integer. The problem is that by default binding would not handle empty string as null value. Fortunately, with .NET 3.5 SP1 there is a way. You should use TargetNullValue attribute. The binding would look like this then:


{Binding Path=Customer.ClientYear, TargetNullValue={x:Static sys:String.Empty}}

This, and other attributes for the binding collected in one document here