2013-06-04 13 views
9

Ich habe folgende Stildefinitionen:PasswordBox nicht davon ausgehen, Stil

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

und die folgende XAML-Code:

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

Jetzt habe ich diese Ergebnisse: result

Die TextBox nehmen der Stil richtig, aber warum die PasswordBox nicht den Stil übernimmt?

+0

Haben Sie versucht, etwas wie [Snoop] (http://snoopwpf.codeplex.com/) zu verwenden, um herauszufinden, wo der Rahmen zur Laufzeit Werte abruft? Es ist möglich, dass etwas die Rahmenstile auf einen höheren [Dependency Property Precedence] setzt (http://msdn.microsoft.com/en-us/library/ms743230.aspx#listing) – Rachel

Antwort

1

Irgendwie Border innerhalb ControlTemplate von PasswordBox nicht MyControlBorder Stil.

Wenn Sie MyPasswordBox Stil wie folgt ändern ... dann wird es funktionieren.

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

Ich weiß es nicht die beste Lösung ist ... aber ich kann nicht herausfinden, warum MyControlBorder nicht angewendet wird. Es funktioniert nicht einmal, wenn Sie MyTextBox Stil loswerden. Dann sind Sie nur noch mit MyControlBorder und MyPasswordBox ... es funktioniert auch nicht.

2

Wenn Sie die Border in einem anderen Border wickeln, funktioniert alles wie erwartet (ich weiß nicht warum).

Als Bonus können Sie jetzt haben PasswordBox es und TextBox es "erben" von der gleichen Style, halten die Dinge schön und trocken.

<!-- 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 ist genial, xaml nicht so genial: P – CRice

+0

Ich fügte auch 'zu MyControlInputBox hinzu, sonst war der Cursor zu hoch. – CRice