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