문제

현재 워터 마크 텍스트가있는 텍스트 상자를 만들고 있으며 약간의 스타일 문제가 있습니다. 워터 마크 자체를 만들기 위해 여기에 설명 된 코드를 포함 시켰습니다.WPF의 워터 마크 / 힌트 텍스트 / 자리 표시 자 텍스트 상자나는 받아 들여진 답을 사용하지 않았지만 가장 높은 표를 가진 답변을 사용했습니다. (Adorner를 사용하는 사람)

내 텍스트 블록은 다음과 같습니다.

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

이제이 첨부 된 속성으로 텍스트 블록이 app.xaml에서 선언 한 스타일에서 범위를 벗어난 문제에 직면 해 있습니다. 스타일은 다음과 같습니다.

<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>

app.xaml의 첨부 된 속성 내에서 텍스트 블록을 어떻게 스타일링 할 수 있습니까?

도움이 되었습니까?

해결책

Declare same style for TextBlock 또한 in Application resources. 이렇게하면 애플리케이션의 모든 텍스트 블록이 장식 자나 창의 일부이든 상관없이 적용됩니다.

<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>

업데이트

리소스를 복제하고 싶지 않다면 사용하는 것이 가장 좋습니다. Label 대신에 TextBlock. 그렇게하면 스타일을 적용 할 수 있습니다 Control 스타일을 도출 할 수 있습니다 Window 그리고 Label 그것을 통해서.

그러나 이것은 효과가 없습니다 TextBlock 그것이 파생되지 않기 때문입니다 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}"/>

그런 다음 TextBlock 대신 AdornerDecorator 내부에 레이블을 사용하면 잘 작동합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top