2013-06-04 66 views
9

我有以下樣式定義:PasswordBox不承擔風格

<!-- Border --> 
<Style x:Key="MyControlBorder" TargetType="{x:Type Border}"> 
    <Setter Property="BorderBrush" Value="DarkKhaki" /> 
    <Setter Property="Background" Value="White" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="CornerRadius" Value="10" /> 
</Style> 

<!-- TextBox --> 
<Style x:Key="MyTextBox" TargetType="{x:Type TextBox}"> 
    <Setter Property="Height" Value="30" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBoxBase}"> 
       <Border Name="TextBoxBorder" Style="{StaticResource MyControlBorder}"> 
        <ScrollViewer x:Name="PART_ContentHost"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- PasswordBox --> 
<Style x:Key="MyPasswordBox" TargetType="{x:Type PasswordBox}"> 
    <Setter Property="Height" Value="30" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Control}"> 
       <Border Name="Border" Style="{StaticResource MyControlBorder}"> 
        <ScrollViewer x:Name="PART_ContentHost" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

和下面的XAML代碼:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <TextBox Grid.Row="0" Style="{StaticResource MyTextBox}" /> 
    <PasswordBox Grid.Row="1" Style="{StaticResource MyPasswordBox}" /> 
</Grid> 

現在我得到這個結果: result

文本框假設風格正確,但爲什麼PasswordBox不承擔風格?

+0

您是否嘗試過使用[Snoop](http://snoopwpf.codeplex.com/)之類的東西來找出邊界從運行時獲取值的位置?有可能是將邊框樣式設置爲更高[Dependency Property Precedence](http://msdn.microsoft.com/en-us/library/ms743230.aspx#listing) – Rachel

回答

1

莫名其妙BorderControlTemplatePasswordBox不採取MyControlBorder樣式。

當你修改MyPasswordBox這樣的風格...那麼它會工作。

<Style x:Key="MyPasswordBox" TargetType="{x:Type PasswordBox}"> 
<Setter Property="Height" Value="30" /> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Control}"> 
      <Border Name="Border" BorderBrush="DarkKhaki" Background="White" BorderThickness="1" CornerRadius="10"> 
       <ScrollViewer x:Name="PART_ContentHost" /> 
      </Border> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

我知道這是不是最好的解決辦法...但我想不出爲什麼不適用MyControlBorder。當你擺脫MyTextBox風格時,它甚至不起作用。然後你只剩下MyControlBorderMyPasswordBox ......它也不起作用。

2

如果將Border換成另一個Border,所有內容都按預期工作(我不知道爲什麼)。

作爲獎勵,您現在可以有PasswordBox es和TextBox es從相同的Style「繼承」,保持良好和乾爽。

<!-- Border Style Definition --> 
<Style x:Key="MyControlBorder" TargetType="Border"> 
    <Setter Property="BorderBrush" Value="DarkKhaki" /> 
    <Setter Property="Background" Value="White" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="CornerRadius" Value="10" /> 
</Style> 

<!-- TextBox and PasswordBox Style --> 
<Style x:Key="MyControlInputBox" TargetType="Control"> 
    <Setter Property="Height" Value="30" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Control}"> 
       <Border> 
        <Border Name="Border" Style="{StaticResource MyControlBorder}"> 
         <ScrollViewer x:Name="PART_ContentHost" /> 
        </Border> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- TextBox --> 
<Style x:Key="MyTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource MyControlInputBox}" /> 

<!-- PasswordBox --> 
<Style x:Key="MyPasswordBox" TargetType="{x:Type PasswordBox}" BasedOn="{StaticResource MyControlInputBox}" /> 
+0

css太棒了,xaml不太好用:P – CRice

+0

我還向MyControlInputBox添加了'',否則光標太高。 – CRice