2013-06-04 22 views
9

tôi có các định nghĩa phong cách sau đây:PasswordBox không thừa nhận phong cách

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

và mã XAML sau:

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

Bây giờ tôi đã nhận kết quả này: result

Các TextBox giả phong cách chính xác, nhưng tại sao PasswordBox không thừa nhận kiểu dáng?

+0

Bạn đã thử sử dụng một cái gì đó như [Snoop] (http://snoopwpf.codeplex.com/) để tìm ra nơi biên giới nhận được giá trị của nó từ lúc chạy? Có thể có điều gì đó đang đặt kiểu viền ở mức cao hơn [Thuộc tính phụ thuộc ưu tiên] (http://msdn.microsoft.com/en-us/library/ms743230.aspx#listing) – Rachel

Trả lời

1

Bằng cách nào đó Border trong phạm vi ControlTemplate của PasswordBox không mất phong cách MyControlBorder.

Khi bạn sửa đổi MyPasswordBox kiểu như thế này ... thì nó sẽ hoạt động.

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

Tôi biết nó không phải là giải pháp tốt nhất ... nhưng tôi không thể hiểu tại sao MyControlBorder không áp dụng. Nó thậm chí không hoạt động khi bạn loại bỏ kiểu MyTextBox. Sau đó, bạn chỉ còn lại MyControlBorderMyPasswordBox ... nó cũng không hoạt động.

2

Nếu bạn quấn Border vào Border mọi thứ khác hoạt động như mong đợi (Tôi không biết tại sao).

Như một phần thưởng, bây giờ bạn có thể có PasswordBox es và TextBox es "kế thừa" từ cùng một Style, giữ mọi thứ đẹp và KHÔ.

<!-- 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 thật tuyệt vời, xaml không quá tuyệt vời: P – CRice

+0

Tôi cũng đã thêm '' vào MyControlInputBox, nếu không con trỏ quá cao. – CRice