如何使密码框无需屏蔽即可获取普通文本 [英] How to make password Box to take normal text without masking
问题描述
我可以让密码盒在不掩盖的情况下使用普通文本,如果是,则如何在wpf中执行此操作。我有一个密码框,应该显示密码输入密码前的文字。输入密码时,它将显示为蒙版文本。请帮忙。
提前致谢
Can I make password Box to take normal text without masking it, if yes how to do it in wpf. I have a password box which should display "Password" Text prior to entering password. And on entering password the it will be displayed as a masked text. Please help.
Thanks in advance
推荐答案
编写自己规范的自定义PasswordBox。在WPF中编写自定义控件非常简单。
示例:
创建WPF自定义控件,第1部分 [ ^ ]
创建WPF自定义控件,第2部分 [ ^ ]
注意:这些是如何在WPF中编写自定义控件的示例。
或尝试下面的样式,将HintTextBoxStyle设置为PasswordBox并在Tag中显示您的消息。
Write a custom PasswordBox of your own specification. Writing custom control in WPF is very easy and simple.
Examples:
Create a WPF Custom Control, Part 1[^]
Create a WPF Custom Control, Part 2[^]
Note: These are examples how to write a custom control in WPF.
or try the below style, Set the HintTextBoxStyle to the PasswordBox and give your message in Tag.
<Style x:Key="HintLabel" TargetType="{x:Type TextBlock}">
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="11" />
<Setter Property="Margin" Value="2,2,0,0" />
<Setter Property="Visibility" Value="Hidden" />
<Setter Property="Foreground" Value="Gray" />
<Setter Property="FontFamily" Value="Aial" />
</Style>
<SolidColorBrush x:Key="ListBorder" Color="#FF7F9DB9"/>
<Style x:Key="HintTextBoxStyle"
TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid Width="Auto" Height="Auto">
<Border x:Name="Bd" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3,3,3,3" BorderBrush="#FF757575" Margin="0,0,0,0"
Background="{TemplateBinding Background}">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" Margin="0,0,0,0"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
<TextBlock x:Name="PART_SearchHintLabel"
Style="{StaticResource HintLabel}"
Text="{TemplateBinding Tag}"
Visibility="Collapsed" />
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="Bd" Value="#FF7AA639"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="Bd" Value="#FF7AA639"/>
</Trigger>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False" />
<Condition Property="Text" Value="" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_SearchHintLabel"
Property="Visibility"
Value="Visible" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False" />
<Condition Property="Text" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_SearchHintLabel"
Property="Visibility"
Value="Visible" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Mark it as Answer, if it is helpful
Hi,
look here at the WPF Watermark PasswordBox:
http://stackoverflow.com/questions/1607066/wpf-watermark-passwordbox-from-watermark-textbox[^]
Hope this is what you are looking for
Hi,
look here at the WPF Watermark PasswordBox:
http://stackoverflow.com/questions/1607066/wpf-watermark-passwordbox-from-watermark-textbox[^]
Hope this is what you are looking for
1. Create UserControl with 2 controls: Textbox and passwordcontrol. One is visible, the other is hidden.
2. Create checkbox to alternate their visibility.
UserControl’s xaml:
1. Create UserControl with 2 controls: Textbox and passwordcontrol. One is visible, the other is hidden.
2. Create checkbox to alternate their visibility.
UserControl's xaml:
<usercontrol x:class="ITWPFControls.WpfPasswordControl" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="35" d:DesignWidth="450">
<grid horizontalalignment="Stretch" margin="3" name="stackPanel1" verticalalignment="Stretch" width="Auto" height="Auto">
<grid.columndefinitions>
<columndefinition width="100*" />
<columndefinition width="Auto" />
</grid.columndefinitions>
<textbox height="Auto" name="TB1" verticalcontentalignment="Center" margin="2" padding="2" grid.column="0" />
<passwordbox height="Auto" name="PWB1" verticalcontentalignment="Center" margin="2" padding="2" grid.column="0" visibility="Hidden" />
<checkbox content="Masked" margin="3" name="CB1" height="Auto" width="Auto" horizontalalignment="Center" horizontalcontentalignment="Center" grid.columnspan="1" verticalcontentalignment="Top" grid.column="1" padding="3,0,3,3" verticalalignment="Center" click="CB1_Click" />
</grid>
</usercontrol>
3. create UserControl’s property \"Password\". Password gets its value from actually visible control
3. create UserControl's property "Password". Password gets its value from actually visible control
public string Password
{
get
{
if (TB1.Visibility == System.Windows.Visibility.Visible) {
return TB1.Text;
} else if (PWB1.Visibility == System.Windows.Visibility.Visible) {
return PWB1.Password;
}
return "";
}
}
4. implement in UserControl’s cs alternating the visiblility:
4.1. the new visible takes its value from old visible
4.2. the new visible gets its carret and focus
4. implement in UserControl's cs alternating the visiblility:
4.1. the new visible takes its value from old visible
4.2. the new visible gets its carret and focus
private void CB1_Click(object sender, RoutedEventArgs e)
{
string _Password = Password;
TB1.Visibility = CB1.IsChecked == true ? System.Windows.Visibility.Hidden : System.Windows.Visibility.Visible;
PWB1.Visibility = CB1.IsChecked == true ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
int _Selection = _Password.Length;
if (_Selection > 0) {
if (TB1.Visibility == System.Windows.Visibility.Visible) {
TB1.Text = _Password;
TB1.SelectionStart = _Selection;
TB1.Focus();
} else if (PWB1.Visibility == System.Windows.Visibility.Visible) {
PWB1.Password = _Password;
MethodInfo _MI = PWB1.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic);
if (_MI != null) {
_MI.Invoke(PWB1, new object[] { _Selection, 0 });
}
PWB1.Focus();
}
}
}
这篇关于如何使密码框无需屏蔽即可获取普通文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!