WPF密码框插入符号
是否有方法隐藏或移动密码框的插入符号?在.NET 3.5 SP1或更早版本中,没有干净的方法来指定WPF文本框/密码框插入符号的颜色 但是,有一种方法可以指定(或者在本例中删除)视图中的插入符号(通过hack)。插入符号颜色与文本框/密码框的背景色相反。因此,您可以将背景颜色设置为“透明黑色”,这将欺骗系统使用白色插入符号(不可见) 代码(简单地)如下所示:WPF密码框插入符号,wpf,caret,passwordbox,Wpf,Caret,Passwordbox,是否有方法隐藏或移动密码框的插入符号?在.NET 3.5 SP1或更早版本中,没有干净的方法来指定WPF文本框/密码框插入符号的颜色 但是,有一种方法可以指定(或者在本例中删除)视图中的插入符号(通过hack)。插入符号颜色与文本框/密码框的背景色相反。因此,您可以将背景颜色设置为“透明黑色”,这将欺骗系统使用白色插入符号(不可见) 代码(简单地)如下所示: <PasswordBox Background="#00000000" /> 有关此问题的更多信息,请查看以下链接:
<PasswordBox Background="#00000000" />
有关此问题的更多信息,请查看以下链接:
希望这有帮助 您可以尝试以下方法来设置密码框中的选择:
private void SetSelection(PasswordBox passwordBox, int start, int length)
{
passwordBox.GetType()
.GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(passwordBox, new object[] { start, length });
}
之后,按如下方式调用以设置光标位置:
// set the cursor position to 2... or lenght of the password
SetSelection( passwordBox1, 2, 0);
// focus the control to update the selection
passwordBox1.Focus();
要获得Passwordbox的选择,我使用以下代码:
private Selection GetSelection(PasswordBox pb)
{
Selection result = new Selection();
PropertyInfo infos = pb.GetType().GetProperty("Selection", BindingFlags.NonPublic | BindingFlags.Instance);
object selection = infos.GetValue(pb, null);
IEnumerable _textSegments = (IEnumerable)selection.GetType().BaseType.GetField("_textSegments", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(selection);
object first_textSegments = _textSegments.Cast<object>().FirstOrDefault();
object start = first_textSegments.GetType().GetProperty("Start", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments, null);
result.start = (int) start.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(start, null);
object end = first_textSegments.GetType().GetProperty("End", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments, null);
result.length = (int)start.GetType().GetProperty("Offset", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(end, null) - result.start;
return result;
}
struct Selection
{
public int start;
public int length;
}
private Selection GetSelection(密码箱pb)
{
选择结果=新选择();
PropertyInfo infos=pb.GetType().GetProperty(“选择”,BindingFlags.NonPublic | BindingFlags.Instance);
对象选择=infos.GetValue(pb,null);
IEnumerable\u textSegments=(IEnumerable)selection.GetType().BaseType.GetField(“\u textSegments”,BindingFlags.NonPublic | BindingFlags.Instance”).GetValue(selection);
object first_textSegments=_textSegments.Cast().FirstOrDefault();
object start=first_textSegments.GetType().GetProperty(“start”,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments,null);
result.start=(int)start.GetType().GetProperty(“偏移量”,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(start,null);
object end=first_textSegments.GetType().GetProperty(“end”,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(first_textSegments,null);
result.length=(int)start.GetType().GetProperty(“偏移量”,BindingFlags.Instance | BindingFlags.NonPublic).GetValue(end,null)-result.start;
返回结果;
}
结构选择
{
公共int启动;
公共整数长度;
}
在.net 4.0上测试,希望这也适用于您。通过.net 4.5.2上的.xaml解决此问题的解决方案:
<PasswordBox Style="{DynamicResource PinEntry}">
然后在PinEntry style下的样式文件上,可以执行以下操作:
<Style x:Key="PinEntry" TargetType="{x:Type PasswordBox}">
...
<Setter Property="CaretBrush" Value="Transparent"/>
...
</Style>
...
...
这实际上是我使用样式的实现,您可以修改代码以满足您的需要。
希望能有所帮助。有没有可能详细说明一下?我知道这是一条老线索,但有人知道4.0中的“可定制克拉”吗?我在文本框上找不到CaretBrush属性:谢谢Brad,我已经搜索过了。Sameer和klaus的回答帮助实现了一个“眼睛”按钮,可以隐藏/显示密码,同时将光标保持在纯文本框和隐藏文本密码框之间的相同位置。