Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf, style is not being applied

Tags:

c#

wpf

xaml

I've written a user control with popup, who's content is being set outside the control. The ControlTemplate of that control looks like the following:

<ControlTemplate TargetType="local:InfoIcon">
    <Grid>
        <ToggleButton x:Name="HelpButton" Style="{StaticResource HelpButton}" />
        <Popup PlacementTarget="{Binding ElementName=HelpButton}" Placement="Bottom"
                IsOpen="{Binding ElementName=HelpButton, Path=IsChecked, Mode=TwoWay}" StaysOpen="False">
            <Border BorderBrush="#767676" BorderThickness="1" 
                    Background="#f1f2f7">
                <Border.Resources>

                    <!-- Important -->
                    <Style TargetType="Label">
                        <Setter Property="Foreground" Value="#575757" />
                    </Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="#575757" />
                    </Style>
                    <!-- /Important -->

                </Border.Resources>
                <ContentPresenter Content="{TemplateBinding HelpContent}" />
            </Border>
        </Popup>                                   
    </Grid>
</ControlTemplate>

The Important part - I want to assign custom styles to items, which are being put inside the popup (it serves as a clickable hint)

I'm using my control in the following way:

<local:MyControl>
    <local:MyControl.HelpContent>
        <TextBlock>Ala ma kota</TextBlock>
    </local:MyControl.HelpContent>
</local:MyControl>

But despite styles in the Border, TextBlock's text's color always inherit the value from its parent (checked using Snoop) - resulting in white text on white background.

You can downlad the small PoC application, which demonstrates the problem.

My observations:

  • The styling does work for Label. It only doesn't work for TextBlock.
  • When I add TextBlock.Foreground="Red" to the Border, TextBlock becomes red, still ignoring style (but now using color from Border).
  • Snoop informs, that this TextBlock actually has the Style resolved correctly. But despite it shouldn't, it uses the inherited value instead of one specified in the style.

How can I solve this problem and why does it occur?

like image 281
Spook Avatar asked Mar 21 '23 08:03

Spook


1 Answers

I received answer on Microsoft forums; I'll leave it here in case someone encounters the same problem.

The difference is that a TextBlock is not a control, i.e. it doesn't have any ControlTemplate and because of this the implicit style doesn't get applied to it when it is located inside the StackPanel. Please see the following page for more information: http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx

You could use Label elements or set the style for the TextBlock elements explicitly.

-- Magnus (MM8)

like image 86
Spook Avatar answered Mar 31 '23 18:03

Spook