2011-05-25 93 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