WPF 密码框标签可见性

发布于 2024-11-09 23:41:45 字数 4181 浏览 2 评论 0原文

我为登录屏幕创建了一个密码框用户控件。我试图使用水印方法,但当我尝试使用它们时,我看到的大多数示例都失败了。我转而只能通过 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>

的控件 C# 代码

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

PasswordTest_PasswordChanged

private void PasswordTest_PasswordChanged(object sender, RoutedEventArgs e)
{

}

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-11-16 23:41:45

您需要在模板中找到它:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文