WPF 密码框标签可见性
我为登录屏幕创建了一个密码框用户控件。我试图使用水印方法,但当我尝试使用它们时,我看到的大多数示例都失败了。我转而只能通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在模板中找到它:
You need to find it in the template: