Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

02 May 2010

Always databind SelectedItem before ItemsSource

The order in which bindings are defined in Xaml are relevant. Look at this Example: I have a ComboBox and a ViewModel. The ItemsSource of the ComboBox is bound to the Repositories property and SelectedItem is bound to SelectedRepository.

<ComboBox ItemsSource="{Binding Repositories}"
          SelectedItem="{Binding SelectedRepository}" />

The constructor of the ViewModel initializes the Repositories with a non empty collection and sets the SelectedRepository to the first element.

public ViewModel()
{
    Repositories = new List<string> {"First", "Second", "Third"};
    SelectedRepository = Repositories[0];
}

Yet, immediately after starting, I got null reference exceptions from other databound properties that are referencing the SelectedRepository property! After a little debugging I found out that when assigning the ViewModel to the DataContext of the view, the Binding Engine assigns null to the SelectedRepository! If you change the declaration of Databinding everything works as expected:

<ComboBox SelectedItem="{SelectedRepository}"
          ItemsSource="Repositories" />
Conclusion: the order of databinding declaration matters!

23 February 2010

Expression Blend 3 Design Time Attributes

Only a few people (including me until a few weeks ago) know that Expression Blend supports so called “Design Time Attributes”. These properties are very valuable to get “Design Time Data” into Blend. Without those example data designing DataTemplates is cumbersome and no fun. Those Design Time Attributes are not documented at all and I found out about them only by chance, reading some blogs. It seems that they are officially introduced with VS 2010 and documented there. Nevertheless they are fully functional with VS 2008 because they are ignored by the compiler and only used by design tools like blend.

All Design Time Attributes live in the d: namespace:

d:DesignHeight

In Blend, there is a special handle you can use to modify the d:DesignHeight and d:DesignWidth attributes without modifying the real Height attribute. This is handy if you just want to test some resizing logic.DesignTimeHeight

d:DesignWidth

DesignTimeWidth
d:DataContext Lets you specify the DataContext used at design time. Example: d:DataContext={StaticResource MyDataContextObject}, or even better    use d:DesignInstance

d:DesignInstance

d:DataContext="{d:DesignInstance Type=local:MyDataContext, IsDesignTimeCreatable=True}"

d:DesignData

needs VS2010, links to a sample data object defined in a xaml file

d:DesignSource

sets the Source property of a CollectionViewSource at design time

12 December 2009

Open Xaml file in Xml view without loading the Designer

I use Expression Blend almost always as a design tool for my WPF projects. If I want to do quick modifications inside the xaml code from Visual Studio, I find the designer loading time quite annoying. But VS offers you the possibility to open xaml files in the xml/xaml view by default. Just open Tools –> Options –> Text Editor –> XAML –> Miscellaneous and tick “Always open documents in full XAML view” check. 
full_xaml_view_vs2008