totoro
totoro

Reputation: 3407

how to access other resources inside a ResourceDictionary

A DataTemplate inside a Resource Dictionary needs to refer to a Styles.xaml, so I have the following

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:src="clr-namespace:WPFApp">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="resources/Styles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate DataType="{x:Type src:MyFileInfo}">
        <Grid>
            grid stuff
        </Grid>

        <TextBlock> stuff </TextBlock>
    </DataTemplate>


</ResourceDictionary>

but there is an error at DataTemplate saying that The proprety "Visual Tree" can only be set once. What does this mean? Is it good practice to put a DataTemplate inside a ResourceDictionary? How to access other resources inside a ResourceDictionary?

Upvotes: 1

Views: 100

Answers (1)

flo_badea
flo_badea

Reputation: 784

A DataTemplate should only have one child. Use this:

<DataTemplate DataType="{x:Type src:MyFileInfo}">
    <Grid>
        grid stuff
        <TextBlock> stuff </TextBlock>
    </Grid>        
</DataTemplate>

Upvotes: 2

Related Questions