开发者

WPF PasswordBox label visibility

开发者 https://www.devze.com 2023-03-08 03:46 出处:网络
I have created a passwordbox user control for a login screen. I was trying to use a watermark approach, but most of the examples I saw failed when I tried to use them. I switched to just being able to

I have created a passwordbox user control for a login screen. I was trying to use a watermark approach, but most of the examples I saw failed when I tried to use them. I switched to just being able to manipulate the label's visibility through c# code.

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

Code for the control

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

C# for PasswordTest_PasswordChanged

private void PasswordTest_PasswordChanged(object sender, RoutedEventArgs e)
{

}

I tried accessing the label but not sure how to exactly. I tried parsing the sender as a passwordbox like other examples use for watermarks, but I am unable to access the password attribute.


You need to find it in the template:

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

精彩评论

暂无评论...
验证码 换一张
取 消