2015-07-21 55 views
0

我的應用程序中有各種文本框,其中我用紅色邊框來驗證輸入的數據。問題是當我改變標籤紅色邊框消失。所以我試圖在我的樣式文件中應用AdornedElementPlaceholder,以便所有文本框都可以適應該行爲。我已經嘗試了下面提到的代碼,但粗體代碼行(AdornedElementPlaceholder)不起作用。我正在使用WPF和C#。任何人都可以幫忙嗎?AdornedElementPlaceholder不工作:驗證邊框在更改標籤時消失

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" > 
       <Setter Property="Width" Value="120" /> 
       <Setter Property="Height" Value="25" /> 
       <Setter Property="FontFamily" Value="Verdana"/> 
       <Setter Property="FontSize" Value="12"/> 
       <Setter Property="VerticalAlignment" Value="Top"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type TextBoxBase}"> 
          <Border x:Name="bg" 
         CornerRadius="5" 
         Padding="2" 
         Background="{TemplateBinding Background}" 
         BorderBrush="{StaticResource NormalBorderBrush}" 
         BorderThickness="1" > 
         **<AdornedElementPlaceholder/>** 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="BorderBrush"       
             TargetName="bg" Value="#82CAFA"/> 
           </Trigger> 
           <Trigger Property="IsFocused" Value="True"> 
            <Setter Property="BorderBrush" 
            TargetName="bg" Value="#1589FF"/> 
           </Trigger> 
           <Trigger Property="IsReadOnly" Value="True"> 
            <Setter Property="Background"      
             Value="#E5E4E2"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="SpellCheck.IsEnabled" Value="True" /> 
       <Setter Property="Language" Value="en-gb" /> 
       <Style.Triggers> 
        <Trigger Property="Validation.HasError" Value="true"> 
         <Setter Property="BorderBrush" Value="{StaticResource 
          ErrorBackground}" /> 
         <Setter Property="ToolTip" Value="{Binding 
         RelativeSource={RelativeSource Self}, Path= 
         (Validation.Errors)[0].ErrorContent}"/> 
        </Trigger> 
       </Style.Triggers>   
      </Style> 

回答

0
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}"> 
    <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/> 
    <Setter Property="Width" Value="120"/> 
    <Setter Property="Height" Value="25"/> 
    <Setter Property="FontFamily" Value="Verdana"/> 
    <Setter Property="FontSize" Value="12"/> 
    <Setter Property="VerticalAlignment" Value="Top"/> 
    <Setter Property="SpellCheck.IsEnabled" Value="True"/> 
    <Setter Property="Language" Value="en-gb"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBoxBase}"> 
       <Border CornerRadius="5" Padding="2" Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1"> 
        <ScrollViewer x:Name="PART_ContentHost" Padding="{TemplateBinding Padding}" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="BorderBrush" Value="#82CAFA"/> 
     </Trigger> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="BorderBrush" Value="#1589FF"/> 
     </Trigger> 
     <Trigger Property="IsReadOnly" Value="True"> 
      <Setter Property="Background" Value="#E5E4E2"/> 
     </Trigger> 
     <Trigger Property="Validation.HasError" Value="True"> 
      <Setter Property="BorderBrush" Value="{StaticResource ErrorBackground}"/> 
      <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

我需要使用adornedplaceholderelement,我的問題是我怎麼能做到這一點在我的代碼。我寫的方式,它不工作 – Manika

+0

我不知道你爲什麼需要使用adornedplaceholderelement。如果你喜歡,可以在ErrorTemplate中使用它。但是如果你只需要紅色邊框,那麼我的回答就可以。 – nicetry

+0

紅色邊框用於突出顯示頁面上的驗證 – Manika

相關問題