2016-11-20 135 views
0

我用它來圓TextBox的角落,WPF PasswordBox圓角

<ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}"> 
     <Border Background="{TemplateBinding Background}" 
      x:Name="Bd" BorderBrush="#FFE6DDDD" 
      BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10"> 
      <ScrollViewer x:Name="PART_ContentHost"/> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/> 
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      </Trigger> 
      <Trigger Property="Width" Value="Auto"> 
       <Setter Property="MinWidth" Value="100"/> 
      </Trigger> 
      <Trigger Property="Height" Value="Auto"> 
       <Setter Property="MinHeight" Value="20"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

我申請,

 <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" 
      Height="25" 
      Margin="168,100,139,194" 
      HorizontalContentAlignment="Center" 
      VerticalContentAlignment="Center" 
      Background="{x:Null}" 
      BorderBrush="{x:Null}" 
      FontFamily="Aller Light">    
     </TextBox> 

任務完成

enter image description here

然後我想在PasswordBox中做,

<ControlTemplate x:Key="passwordbox" TargetType="{x:Type PasswordBox}"> 
     <Border Background="{TemplateBinding Background}" 
      x:Name="Bd" BorderBrush="#FFE6DDDD" 
      BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10"> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/> 
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      </Trigger> 
      <Trigger Property="Width" Value="Auto"> 
       <Setter Property="MinWidth" Value="100"/> 
      </Trigger> 
      <Trigger Property="Height" Value="Auto"> 
       <Setter Property="MinHeight" Value="20"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

申請

<PasswordBox Template="{StaticResource passwordbox}" 
       Height="25" 
       Margin="168,140,139,154" 
       HorizontalContentAlignment="Center" 
       VerticalContentAlignment="Center" 
       Background="{x:Null}" 
       Password="someonepass"> 
    </PasswordBox> 

這似乎是成功的,但不能輸入的內容。 (第二個框)

enter image description here

如果我刪除模板,通常

enter image description here

如何解決呢?謝謝...

+0

看起來你忘記了一個'PART_'。 –

回答

0

您的PasswordBox控制模板遺漏了一個名爲「PART_ContentHost」的部件(最終爲ScrollViewer)。以您的TextBoxBase模板爲例。

所以你temaplate應該是:

<ControlTemplate x:Key="passwordbox" TargetType="{x:Type PasswordBox}"> 
    <Border Background="{TemplateBinding Background}" 
     x:Name="Bd" BorderBrush="#FFE6DDDD" 
     BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10"> 
     <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
    </Border> 
    <ControlTemplate.Triggers> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
     </Trigger> 
     <Trigger Property="Width" Value="Auto"> 
      <Setter Property="MinWidth" Value="100"/> 
     </Trigger> 
     <Trigger Property="Height" Value="Auto"> 
      <Setter Property="MinHeight" Value="20"/> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

我希望它可以幫助你。