2011-09-26 2 views
0

다음과 같이 간단한 TextBox Validation.ErrorTemplate을 선언했습니다.Validation.ErrorTemplate에서 TextBlock의 FontSize 바인딩

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <DockPanel LastChildFill="True"> 
        <TextBlock Text="!" DockPanel.Dock="Right" 
           FontSize="{TemplateBinding TextBox.FontSize}" 
           Foreground="Red"/> 
        <AdornedElementPlaceholder Name="adornerPlaceholder" /> 
       </DockPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

나는 느낌표의 글꼴 크기가 텍스트 상자와 같은 글꼴 (편집) 크기 것으로 기대하지만, 기대 발생하지 않습니다 항상 기본 글꼴 크기를 가져옵니다. 또한 RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FontSize을 사용하여 바인딩을 시도했지만 문제를 해결할 수도 없습니다. 왜 이런 상황이 발생 했습니까? 느낌표를 TextBox와 동일한 크기로 만들려면 어떻게해야합니까?

+0

당신의 탐구는 약간 모호합니다. 텍스트 상자에 적용되는 글꼴 크기에 바인딩하려고합니까? 아니면 텍스트 상자 크기에 바인딩하려고합니까? 이전 텍스트 상자에 다른 글꼴 크기를 적용한 경우? – CBRRacer

+0

@CBRRacer :이 질문은 귀하가 언급 한 것이 아닙니다. 나는 TextBox의 텍스트가 유효하지 않을 때 TextBox 바로 오른쪽에 느낌표가 나타나고 느낌표의 글꼴 크기는 TextBox와 같은 글꼴 크기를 얻으려고합니다. –

답변

1

AdornedElementPlaceholder에 바인딩하지 않습니까?

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
<Setter Property="Validation.ErrorTemplate"> 
    <Setter.Value> 
     <ControlTemplate> 
      <DockPanel LastChildFill="True"> 
       <TextBlock Text="!" DockPanel.Dock="Right" 
          FontSize="{Binding ElementName=adornerPlaceholder, Path=AdornedElement.FontSize}" 
          Foreground="Red"/> 
       <AdornedElementPlaceholder Name="adornerPlaceholder" /> 
      </DockPanel> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 
</Style> 

이 검증되지 않은,하지만 작동합니다 :)

+0

나는 쉬운 방법이 있음을 알았다. 고마워. –

0

또 다른 옵션이 자동으로 장식 요소와 함께 높이를 확장되는 Viewbox,에 TextBlock을 래핑하는 것입니다 :

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <DockPanel LastChildFill="True"> 
        <Viewbox DockPanel.Dock="Right" 
         Height="{Binding ElementName=adornerPlaceholder, Path=ActualHeight}" 
         Stretch="Uniform" 
         Margin="5 0"> 
         <TextBlock Text="!" Foreground="Red" /> 
        </Viewbox> 
        <AdornedElementPlaceholder Name="adornerPlaceholder" /> 
       </DockPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

이것은 글꼴 크기, 느낌표 그래픽 (예 : 텍스트, 경로, 요소 등)에 관계없이 장식되는 모든 요소에 대해 작동합니다.

위치/레이아웃을 여백으로 조정할 수 있습니다.