2011-05-25 92 views
1

我爲登錄屏幕創建了密碼箱用戶控件。我試圖使用水印方法,但是當我嘗試使用它們時,大部分示例都失敗了。我轉而只能通過c#代碼操縱標籤的可見性。WPF PasswordBox標籤可見性

<Style x:Key="{x:Type PasswordBox}" 
     x:Name="Style1" 
     BasedOn="{StaticResource {x:Type PasswordBox}}" 
     TargetType="{x:Type PasswordBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type PasswordBox}"> 
       <Border x:Name="TextBoxBorder" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         CornerRadius="7"> 
        <Border.Background> 
         <LinearGradientBrush StartPoint="0,0" EndPoint="2,1"> 
           <GradientStop Color="{Binding Path=GradientColorStart}" 
              Offset="0"/> 
           <GradientStop Color="{Binding Path=GradientColorEnd}" 
              Offset="1"/> 
         </LinearGradientBrush> 
        </Border.Background> 
        <Grid> 
         <Label x:Name="TextPrompt" 
           Content="Password" 
           Focusable="False" 
           FontSize="15" 
           Foreground="Green" 
           Visibility="Visible" /> 
         <ScrollViewer x:Name="PART_ContentHost" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
        </Grid> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsFocused" Value="True"> 
         <Setter Property="Foreground" 
           Value="{Binding Path=OnFocusTextColor}" /> 
         <Setter Property="FontWeight" 
           Value="{Binding Path=OnFocusFontWeight}" /> 
         <Setter Property="FontStyle" 
           Value="{Binding Path=OnFocusFontStyle}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      <!-- 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsFocused" Value="False"/> 
          <Condition Property="Password" Value=""/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Visibility" 
           TargetName="TextPrompt 
           Value="Visible"/> 
        </MultiTrigger> 
        <Trigger Property="IsFocused" Value="True"> 
         <Setter Property="Foreground" Value="{Binding Path=OnFocusTextColor}" /> 
         <Setter Property="FontWeight" Value="{Binding Path=OnFocusFontWeight}" /> 
         <Setter Property="FontStyle" Value="{Binding Path=OnFocusFontStyle}" /> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Property="Foreground" Value="DimGray" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      --> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

代碼控制

<PasswordBox x:Name="PasswordTest" 
    FontSize="15" 
    Padding="{Binding Path=TextPadding}" 
    Tag="{Binding Path=TextValue}" 
    PasswordChanged="PasswordTest_PasswordChanged"> 
</PasswordBox> 

C#爲PasswordTest_PasswordChanged

private void PasswordTest_PasswordChanged(object sender, RoutedEventArgs e) 
{ 

} 

我試圖訪問標籤,但不知道如何準確。我試圖解析發件人作爲密碼箱像其他例子用於水印,但我無法訪問密碼屬性。

回答

0

你需要找到它的模板:

Label textPrompt = null; 
if (sender is PasswordBox && ((PasswordBox)sender).Template != null) 
    textPrompt = ((PasswordBox)sender).Template.FindName("TextPrompt", (PasswordBox)sender) as Label; 
+0

這並沒有似乎工作。它表示該資源爲空。 – MeisterGao 2011-05-26 13:55:23

+0

您能更具體地瞭解您遇到的錯誤嗎?什麼說那個資源是空的? – 2011-05-26 21:52:41

+0

獲取錯誤: 「System.Windows.FrameworkTemplate.FindName(string,System.Windows.FrameworkElement)'的最佳重載方法匹配'有一些無效參數」 – MeisterGao 2011-05-27 16:39:54