I have a situation in which I'm creating a custom DataTemplate using XamlReader.Parse(xamlString), where xamlString is the fragment which contains the DataTemplate:
<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<StackPanel Visibility="{Binding MyBinding, Converter={StaticResource boolToVisibilityConverter}}">
...
</StackPanel>
</DataTemplate>
As you can see, this DataTemplate has a reference to a static resource (the BooleanToVisibilityConverter). The call to XamlReader.Parse completes without exception, and I assign its result (a DataTemplate object) to an object in the scene hierarchy (in this case, a GridViewColumn.CellTemplate). But for some reason in the call to MainWindow.Show() I get the exception:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number 'x' and line position 'y'.'
Inner Exception:
Exception: Cannot find resource named 'boolToVisibilityConverter'. Resource names are case sensitive.
Why can't the loaded XAML fragment reference an existing resource in the page?
When the XAML fragment gets created initially I don't think it knows anything about the parent container that your going to place it in, including the static resources defined in the parent. Try to reference the boolToVisibilityConverter inside the DataTemplate using DataTemplate.Resources instead.