Dailycode.info

Short solution for short problems

'Resources' property has already been set on

I was trying to build a nice WPF application, where I use general styles for some buttons. Then at a moment in time I got this nise error: ''Resources' property has already been set on. Complete it looked like this: ''Resources' property has already been set on 'AllAudioView'.' Line number '14' and line position '11'.
So I looked around and finally found what the problem was.
In my code I used a referenced resource file combined with some static resources. It looked like this:
<UserControl.Resources>
    <ResourceDictionary x:Key="Resc">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Style.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <CollectionViewSource x:Key="AudioRecords" Source="{Binding Path=AllAudioRecords}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Number" Direction="Descending" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>
 
As  you see there is a reference to a resource dictionary and behind this a collection view source, which is only used for this control, so it makes sense to put it there. But Microsoft doesn’t think so.
So the only way to solve this is when you reference a resource dictionary, put all your other resources in a separate dictionary file.
I created a second file only for the usercontrol and named it: AllAudioViewResources.xaml. I put all the resources belonging to the usercontrol in this file.
Then I replaced the code in the usercontrol to look like this:
<UserControl.Resources>
    <ResourceDictionary x:Key="Resc">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Style.xaml" />
            <ResourceDictionary Source="AllAudioViewResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
 
And yes, now it works!
 
The resource dictionary file looks like this:

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

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

                    >

   

    <CollectionViewSource x:Key="AudioRecords" Source="{Binding Path=AllAudioRecords}">

        <CollectionViewSource.SortDescriptions>

            <scm:SortDescription PropertyName="Number" Direction="Descending" />

        </CollectionViewSource.SortDescriptions>

    </CollectionViewSource>

</ResourceDictionary>