Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom resource dictionary inside ControlTemplate or DataTemplate

EDIT: This problem occurs when using the standard .NET ResourceDictionary as well and appears to be an issue with using resource dictionaries inside control or data templates.

I have a custom resource dictionary that follows a common approach to sharing resource instances.

http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html

public class SharedResourceDictionary : ResourceDictionary
{
    static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();

    Uri _sourceUri;

    public new Uri Source
    {
        get
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
                return base.Source;

            return this._sourceUri;
        }
        set
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
            {
                base.Source = value;
                return;
            }

            this._sourceUri = value;

            WeakReference<ResourceDictionary> cached;
            if (SharedDictionaries.TryGetValue(value, out cached))
            {
                ResourceDictionary rd;
                if (cached.TryGetTarget(out rd))
                {
                    this.MergedDictionaries.Add(rd);
                    return;
                }
            }

            base.Source = value;
            SharedDictionaries[value] = new WeakReference<ResourceDictionary>(this);
        }
    }
}

It works fine, but whenever it is referenced inside a Resources element within a ControlTemplate or DataTemplate, there are spurious errors shown (these do not affect the build, which still succeeds).

This one gets shown for the standard ResourceDictionary which contains SharedResourceDictionary in its merged dictionaries:

Unable to cast object of type 'Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode' to type 'System.Windows.ResourceDictionary'

Sample XAML:

<DataTemplate DataType="{x:Type vm:MyViewModel}">
    <DockPanel Style="{DynamicResource MainDockPanel}">
        <DockPanel.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <p:SharedResourceDictionary Source="/MyAssembly;component/MyResources.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </DockPanel.Resources>
    </DockPanel>
</DataTemplate>

Does anyone have any ideas how I can eliminate this nuisance error?

Thanks

like image 904
Stephen Drew Avatar asked Nov 22 '12 08:11

Stephen Drew


1 Answers

This issue has been reported to Microsoft. You can vote on it, so maybe it will get fixed in some future release.

https://connect.microsoft.com/VisualStudio/feedback/details/772730/xaml-designer-broken-when-adding-resource-dictionaries-to-data-or-control-templates

like image 98
LawMan Avatar answered Sep 20 '22 17:09

LawMan