修改PasswordBox的占位文本前景色。

huangapple go评论76阅读模式
英文:

Change PasswordBox PlaceHolder Text Foreground color

问题

我有一个PasswordBox控件:

<PasswordBox 
    MaxLength="12" 
    PlaceholderText="Password" 
    Foreground="Black" 
    Background="#eeeeee"/>

我想要像在常规的TextBox上一样更改PlaceholderForeground颜色,但是没有这样的属性,在互联网上搜索后,我找不到适合我的解决方案,而且我也不理解,所有的xaml代码都太长,包括50行的样式和资源,用于如此简单的属性更改,我不知道为什么WPF必须如此复杂,没有理由。

有人能提供一个解决方案,可能不需要50行xaml代码,只需简单地更改PasswordBox中占位文本的前景颜色吗?

英文:

I have a PasswordBox

&lt;PasswordBox 
    MaxLength=&quot;12&quot; 
    PlaceholderText=&quot;Password&quot; 
    Foreground=&quot;Black&quot; 
    Background=&quot;#eeeeee&quot;/&gt;

And I want to change the PlaceholderForeground color like I would on a regular TextBox but there is no property like that and after searching through the internet I found no solution that A worked for me B I understood, all the xaml code was too lengthy with style and resources of 50 lines all for such a simple property change I don't know why WPF has to be so overly complicated for no reason

Can anybody sugguest a solution that is possibly not 50 lines of xaml code that simply changes the foreground color of the placeholder text in the PasswordBox.

答案1

得分: 1

不能在不创建自定义内容的情况下更改占位符颜色。但是有一些绕过方法,而不需要大量的XAML标记。

我想到的最简单的方法是创建一个从PasswordBox派生的自定义控件。你只需要重写OnRender方法:

  1. 调用base.OnRender

  2. 检查密码框中是否没有文本,并且没有焦点。

  3. 创建一个FormattedText对象,可以像这样:

var placeholder = new FormattedText(
  PlaceholderText,
  CultureInfo.CurrentCulture,
  FlowDirection.LeftToRight,
  new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
  FontSize,
  new SolidColorBrush(Color.Blue),  // 或者任何你想要的颜色
  VisualTreeHelper.GetDpi(this).PixelsPerDip);
  1. 在内容的左上角绘制它:
drawingContext.DrawText(placeholder, new Point(Padding.Left, Padding.Top));

如果你不想要任何额外的代码,我知道的唯一方法是使用第三方库,例如MahApps.Metro

你也可以为内置的PasswordBox创建自定义行为,尽管这并不比自定义控件简单。

英文:

You can't change the placeholder color without creating something custom. But there are workarounds without huge snippets of XAML markup.

The easiest way that came to my mind is creating a custom control that derives from PasswordBox. You'll only have to override OnRender:

  1. Call base.OnRender

  2. Check that there's no text in the password box, and it isn't focused

  3. Create a FormattedText object, maybe like this:

var placeholder = new FormattedText(
  PlaceholderText,
  CultureInfo.CurrentCulture,
  FlowDirection.LeftToRight,
  new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),
  FontSize,
  new SolidColorBrush(Color.Blue),  // Or whatever you want
  VisualTreeHelper.GetDpi(this).PixelsPerDip);
  1. Draw it at the top-left corner of the content:
drawingContext.DrawText(placeholder, new Point(Padding.Left, Padding.Top));

If you don't want any extra code, the only way I know is using third-party libraries, e.g. MahApps.Metro.

You can also create a custom behavior for the built-in PasswordBox, though it's not much simpler than a custom control.

huangapple
  • 本文由 发表于 2023年6月19日 04:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定