I need to make password box as non-editable in wpf. I used
IsEnabled = false
But it is affecting my style some blur effect came because of that... Is there any other way to achieve this ?
You can handle the PreviewTextInput
event, preventing the user from entering text. Like so:
Xaml:
<PasswordBox PreviewTextInput="HandleInput"/>
Codebehind:
private void HandleInput(object sender, TextCompositionEventArgs e) {
e.Handled = true;
}
One solution is to make a custom functionality to mimic the IsReadOnly
.
There are couple things to take care of - eg
clipboard pasting
also.
You'll get similar behavior
by defining some attached property (eg IsPasswordReadOnly
or just the same) - which would work out all that's required.
Here is a good starting example - which could, should I think work for Password box as well - but I haven't tried it and you gotta test yourself.
Readonly textbox for WPF with visible cursor (.NET 3.5)
You'd have to replace references to TextBox
with PasswordBox
, rename it to IsReadOnly - and I think the rest might work the same.
And you use it like...
<PasswordBox my:AttachReadOnly.IsReadOnly="True" />
我知道这已经两年了,但是我有相同的需求并以这种方式解决了这两个问题:这两个属性的组合都设置为false,将阻止控件中的输入/编辑,而不会影响所需的样式: ,IsHitTestVisible
Pretty simple..
Set an event handler for PreviewTextInput in your XML like so:
PreviewTextInput="PasswordBoxOnPreviewTextInput"
And within that method:
private void PasswordBoxOnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (m_DisablePasswordBox)
e.Handled = true;
}
It will now prevent you from typing anything in :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.