Search code examples
wpfresourcedictionarycode-behind

ResourceDictionary Adding x:Class


In a first attempt to create a DataTemplate I added the class from MainWindow to access eventhandlers. Now this may be incorrect for a number of reasons (and produces some interesting error messages) however I'd like to understand why a ResourceDictionary is unable to reference a partial class such as MainWindow ?

xaml as follows (note that this fails without any events implemented)

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Window.Resources>
    <ResourceDictionary>
            <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary>
</Window.Resources>
</Window>

Resource dic.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow"
>

</ResourceDictionary>

Any thoughts much appreciated


Solution

  • You cannot have a resource dictionary backed up by something like a MainWindow because MainWindow does not inherit from ResourceDictionary class.

    See this article for better understanding of the use of code behind resource dictionaries ...

    I hope this answers your question.