Click here to Skip to main content
15,112,662 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating DatTemplates for two classes in a resource dictionary. I want to be able to apply a uniform style (here for TextBlock) to both the DataTemplates. But when I run the application I do not see the style applied. If I put the styles in each of the datatemplates then I get key duplication error. How do I accomplish this.

HTML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:TestDataTemplateResources">
    
    <ResourceDictionary.MergedDictionaries>
        
        <ResourceDictionary>
            <Style TargetType="TextBlock">
                <Setter Property="Background" Value="Yellow"/>
                <Setter Property="FontStyle" Value="Italic"/>
            </Style>
        </ResourceDictionary>

        <ResourceDictionary>

            <DataTemplate DataType="{x:Type my:PersonClass}">
            
                <Border BorderBrush="Blue" BorderThickness="2">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                       
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Name}"/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Age}"/>


                    </Grid>
                </Border>
            </DataTemplate>

            <DataTemplate DataType="{x:Type my:AddressClass}">

                <Border BorderBrush="Blue" BorderThickness="2">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Street}"/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding Zip}"/>

                    </Grid>
                </Border>
            </DataTemplate>

        </ResourceDictionary>
        
    </ResourceDictionary.MergedDictionaries>
    
</ResourceDictionary>
Posted
Updated 10-Dec-13 4:44am
v2

try to remove MergedDictionaries at all. Just place style and both data templates in the root ResourceDictionary. Or at least in the same single merged dictionary.
   
Hi Irina, I tried your suggestion and placed everything in the root resourcedictionary. But still the datatemplates do not pick up the style.

HTML
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:TestDataTemplateResources">
    
                     <style targettype="TextBlock">
                        <setter property="Background" value="Yellow" />
                        <setter property="FontStyle" value="Italic" />
                     </style>


            <datatemplate datatype="{x:Type my:PersonClass}">

        
            <border borderbrush="Blue" borderthickness="2">
                    <grid>
                        <grid.columndefinitions>
                            <columndefinition width="Auto" />
                        </grid.columndefinitions>
                       
                        <grid.rowdefinitions>
                            <rowdefinition height="Auto" />
                            <rowdefinition height="Auto" />
                        </grid.rowdefinitions>
                        
                        <textblock grid.column="0" grid.row="0" text="{Binding Name}" />
                        <textblock grid.column="0" grid.row="1" text="{Binding Age}" />


                    </grid>
                </border>
            </datatemplate>

            <datatemplate datatype="{x:Type my:AddressClass}">

                <border borderbrush="Blue" borderthickness="2">
                    <grid>
                        <grid.columndefinitions>
                            <columndefinition width="Auto" />
                        </grid.columndefinitions>

                        <grid.rowdefinitions>
                            <rowdefinition height="Auto" />
                            <rowdefinition height="Auto" />
                        </grid.rowdefinitions>

                        <textblock grid.column="0" grid.row="0" text="{Binding Street}" />
                        <textblock grid.column="0" grid.row="1" text="{Binding Zip}" />

                    </grid>
                </border>
            </datatemplate>
 

    
</resourcedictionary>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900