0

I am currently creating a TextBox with a watermark text and have a little styling problem. To create the Watermark itself I have included the code explained in here Watermark / hint text / placeholder TextBox in WPF I did not use the accepted answer, but the one with the highest votes. (the one using Adorner)

My textblock looks like this:

<AdornerDecorator>
    <TextBox HorizontalAlignment="Right"
                VerticalAlignment="Center"
                Width="190"
                Padding="16,2,20,2">
        <utils:WatermarkService.Watermark>
            <TextBlock Text="Search" />
        </utils:WatermarkService.Watermark>
    </TextBox>
</AdornerDecorator>

Now I face the problem that with this attached property, the textblock in it gets out of scope from my styling I have declared in app.xaml. The styling looks like this:

<Style TargetType="{x:Type Window}">
    <Setter Property="FontFamily"
            Value="Tahoma" />
    <Setter Property="FontSize"
            Value="8pt"></Setter>
    <Setter Property="Background"
            Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
</Style>

How is it possible to style the textblock within the attached property in app.xaml, preferable with basedon this style so I dont have to declare it serval times.

Community
  • 1
  • 1
Rand Random
  • 7,300
  • 10
  • 40
  • 88

1 Answers1

1

Declare same style for TextBlock as well in Application resources. This way it will be applied to all TextBlocks in your application no matter whether they are part of Adorners or window.

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="FontFamily"
           Value="Tahoma" />
   <Setter Property="FontSize"
           Value="8pt"></Setter>
   <Setter Property="Background"
         Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
</Style>

UPDATE

If you don't want to duplicate resources, best you can get is use Label instead of TextBlock. That way you can have style applied on Control and can derive styles for Window and Label from that.

But this won't work for TextBlock since it doesn't derive from Control.

   <Style TargetType="Control" x:Key="BaseStyle">
        <Setter Property="FontFamily" Value="Tahoma" />
        <Setter Property="FontSize" Value="8pt"></Setter>
        <Setter Property="Background" 
        Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
    </Style>

    <Style TargetType="{x:Type Window}"
           BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="{x:Type Label}"
           BasedOn="{StaticResource BaseStyle}"/>

Then if you use Label inside AdornerDecorator in place of TextBlock, it will work fine.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks for the answer. Isnt there an easier way to specify a fontsize/fontfamily thats used default for all text. I kinda hoped its with my styling, but this doesnt seem the case. And I dont like the idea to specify the same styling twice cant I used atleast a basedon style in my case? – Rand Random Nov 08 '13 at 09:06
  • For BasedOn to work targetType should be same for both styles. But in one case you have Window and other is TextBlock. – Rohit Vats Nov 08 '13 at 13:03
  • So its not possible without duplicate styling? – Rand Random Nov 08 '13 at 13:04
  • Updated with another approach where you can use Label in place of TextBlock where you don't have to duplicate styles. – Rohit Vats Nov 08 '13 at 13:28