WPF PasswordBox标签可见性

时间:2011-05-25 16:08:12

标签: wpf label visibility passwordbox

我为登录屏幕创建了一个密码箱用户控件。我试图使用水印方法,但是当我尝试使用它们时,我看到的大多数示例都失败了。我切换到能够通过c#代码操纵标签的可见性。

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

PasswordTest_PasswordChanged

的C#
private void PasswordTest_PasswordChanged(object sender, RoutedEventArgs e)
{

}

我尝试访问标签,但不确定如何准确。我尝试将发件人解析为密码框,就像其他用于水印的示例一样,但我无法访问密码属性。

1 个答案:

答案 0 :(得分:0)

您需要在模板中找到它:

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