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}" 
/>

Properties defaults for a Binding object in WPF

Default values for a Binding object used to bind a TextBox. No property has been changed neither on Text nor on the Binding itself. Inside the Binding we can see a custom ValidationRule defined.

Default values for a binding object

TextBlock vs Label in WPF

A question that most probably every WPF developer asked himself is why have Label and TextBlock controls in WPF?

Doing a simple google search gave this answer:

Label is a more complex control than TextBlock and it has features like access keys (mnemonics) and target controls. So when you hit Alt-Key than the target control get focus (e.g. a label and a textbox).

This difference resides in the fact that TextBlock, even though it is included in Windows.System.Controls namespace, it is not a control. It inherits FrameworkElement class not the Control class. Label on the other hand it inherits from Control class.

For more details I would suggest a post on “John Smith On WPF” blog:

http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/

Follow

Get every new post delivered to your Inbox.