1

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?

totoro
  • 3,257
  • 5
  • 39
  • 61
  • 1
    Do you actually have `"x:Type src:MyFileInfo"` or `"{x:Type src:MyFileInfo}"`? Also refer to [this](http://stackoverflow.com/questions/22965447/the-property-visualtree-is-set-more-than-once) question. Problem may be in _data template stuff_ – dkozl Oct 21 '14 at 09:04
  • oops, fixed. I think you are right; the error went away after deleting `data template stuff`... – totoro Oct 21 '14 at 09:10
  • Can you show your `DataTemplate`? Error would suggest that it contains more then one root element – dkozl Oct 21 '14 at 09:12
  • @dkozl thanks, that post solved my problem. can I still delete this question? since the problem isn't really in the code – totoro Oct 21 '14 at 09:13
  • @dkozl guess not. I'll edit the error in now ;) – totoro Oct 21 '14 at 09:17

1 Answers1

2

A DataTemplate should only have one child. Use this:

<DataTemplate DataType="{x:Type src:MyFileInfo}">
    <Grid>
        grid stuff
        <TextBlock> stuff </TextBlock>
    </Grid>        
</DataTemplate>
flo_badea
  • 774
  • 5
  • 8