Search code examples
wpfstylestooltipdatatemplatecontenttemplate

Wpf Style for tooltip isn't applied


I have this situation:

I defined a style for a tooltip of my custom component:

<Style x:Key="ToolTipPieChart" TargetType="{x:Type ToolTip}">
    <Setter Property="Opacity" Value=".95" />
    <Setter Property="Template">
        <Setter.Value>
            <!-- modify the tooltip control template to add a drop shadow-->
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Grid Background="Transparent" Margin="5" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                    <Rectangle Fill="White" Height="{TemplateBinding Height}" RadiusX="7.5" RadiusY="7.5">
                        <Rectangle.BitmapEffect>
                            <DropShadowBitmapEffect ShadowDepth="3"/>
                        </Rectangle.BitmapEffect>
                    </Rectangle>
                    <ContentPresenter Margin="5"  HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <!-- bind the stack panel datacontext to the tooltip data context -->
                <StackPanel Orientation="Horizontal"
                DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}">

                    <!-- navigate to the pie piece (which is the placement target of the tooltip) and obtain the percentage -->
                    <TextBlock FontSize="30" FontWeight="Bold" Margin="0,0,5,0"                        
                    DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"
                    Text="{Binding Path=Percentage, Converter={StaticResource formatter}, ConverterParameter='\{0:0%\}'}"/>

                    <StackPanel Orientation="Vertical">
                        <TextBlock FontWeight="Bold"  Text="{Binding Path=AFCDENTE}"/>
                        <!--<StackPanel Orientation="Horizontal">
                    <TextBlock Name="_txbEnte" Text="AFCDENTE"/>
                    <TextBlock Text=": "/>
                    <TextBlock Text="{Binding Path=AFCDENTE}"/>
                </StackPanel>-->
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbModGen" Text="Modgen"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=Modgen}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbAggAlter" Text="AggAlter"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=AggAlter}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbAggTolt" Text="AggTolt"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=AggTolt}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbModDis" Text="ModDis"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=ModDis}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbModPar" Text="ModPar"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=ModPar}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbCamPrior"  Text="CamPrior"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=CamPrior}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbCamCL" Text="CamCL"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=CamCL}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbCamMat" Text="CamMat"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=CamMat}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbModTTechP" Text="ModTTechP"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=ModTTechP}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Name="_txbModTRianL" Text="ModTRianL"/>
                            <TextBlock Text=": "/>
                            <TextBlock Text="{Binding Path=ModTRianL}"/>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

And here's my component:

<PieChart:PieChartLayout HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0"
    x:Name="piePlotter" PlottedProperty="Totale" DisplayProperty="AFCDENTE" Margin="10">
    <PieChart:PieChartLayout.ColorSelector>
        <PieChart:IndexedColourSelector Brushes="{StaticResource brushes}"/>
    </PieChart:PieChartLayout.ColorSelector>
    <PieChart:PieChartLayout.ToolTip>
        <ToolTip Style="{StaticResource ToolTipPieChart}" />
    </PieChart:PieChartLayout.ToolTip>
</PieChart:PieChartLayout>

The style isn't applied. Why in your opinion? What should I do?


Solution

  • How about defining the ToolTip Style inside the actual PieChartLayout.Resources and omnitting the x:Key Directive?:

    <PieChart:PieChartLayout HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0"
        x:Name="piePlotter" PlottedProperty="Totale" DisplayProperty="AFCDENTE" Margin="10">
        <PieChart:PieChartLayout.Resources>
            <!-- Define your ToolTip Style here WITHOUT setting x:Key Directive -->
            <Style TargetType="{x:Type ToolTip}">
                ...
            </Style>
        </PieChart:PieChartLayout.Resources>
        <PieChart:PieChartLayout.ColorSelector>
            <PieChart:IndexedColourSelector Brushes="{StaticResource brushes}"/>
        </PieChart:PieChartLayout.ColorSelector>
        <PieChart:PieChartLayout.ToolTip>
            <ToolTip />
        </PieChart:PieChartLayout.ToolTip>
    </PieChart:PieChartLayout>