Provide value on ‘System.Windows.Data.Binding’ threw an exception

Today is the second time I receive the exception message: “Provide value on ‘System.Windows.Data.Binding’ threw an exception” when using the WPF DataTemplateSelector.

Problem: Static resources declaration order *matter* !!! So you should not declare the instance of your DataTemplateSelector before actually declaring DataTemplates (see code sample below).

	
<!-- data template selector NOT HERE-->
<local:CustomDataTemplateSelector
	x:Key="_customDataTemplateSelector"
	FirstDataTemplate="{StaticResource _firstDataTemplate}"
	SecondDataTemplate="{StaticResource _secondDataTemplate}"
	ThirdDataTemplate="{StaticResource _thirdDataTemplate}"
/>

<DataTemplate x:Key="_firstDataTemplate"> 
	<TextBlock Text="dataTemplate 1"/> 
</DataTemplate> 

<DataTemplate x:Key="_secondDataTemplate"> 
	<TextBlock Text="dataTemplate 2"/> 
</DataTemplate> 

<DataTemplate x:Key="_thirdDataTemplate"> 
	<TextBlock Text="dataTemplate 3"/> 
</DataTemplate> 

<!-- data template selector --> 
<local:CustomDataTemplateSelector 
	x:Key="_customDataTemplateSelector" 
	FirstDataTemplate="{StaticResource _firstDataTemplate}" 
	SecondDataTemplate="{StaticResource _secondDataTemplate}" 
	ThirdDataTemplate="{StaticResource _thirdDataTemplate}" 
/>

.NET Task class

Hey! Just to give you a heads up for a new class called Task introduced in .Net Framework 4.

This class, as the name suggests, allows the developer to run a synchronous or asynchronous job in a separate thread. It offers quite a few advantages in comparison with other well known classes like the now old BackgroundWorker like the possibility to cancel a task and the possibility to create sub-tasks and many others. In exchange what you see in BackgroundWorker and you don’t see in Task class is progress reporting. Fortunately this is a functionality that is easily implementable also using a Task class. See this nice post for how to do it. You can also find there an equivalent implementation using BackgroundWorker.

Task class is found inside System.Threading.Tasks namespace. This class and related types are part of a set of public types called TPL (Task Parallel Library) and it is a new programming model introduced by Microsoft in it’s framework mainly for parallel programming.

For more information please visit MSDN page for TPL (Task Parallel Library).

Here’s a simple example (from MSDN) of code showing how you can manipulate a single Task instance:

Read the rest of this entry »

Follow

Get every new post delivered to your Inbox.