UWP - 如何将 CharacterSpacing 应用于 PlaceholderText

UWP - How to apply CharacterSpacing to PlaceholderText

我想知道有什么方法可以将 CharacterSpacing 应用于 TextBox 中的 PlaceholderText 吗?

我尝试编辑 TextBox 模板,我注意到 ContentPresenter 正在显示 PlaceholderText,我尝试在其上使用 CharacterSpacing,但它不起作用。另外,我尝试了 FontStretch,但还是没有结果。

I tried to edit TextBox template and I noticed a ContentPresenter is showing PlaceholderText, I tried to use CharacterSpacing on it, but it did not work

里面的PlaceholderText里面有一个ContentControl for the when use default style. And PlaseholderText is the value of content property. The type of content property is object, in the meanwhile, CharaterSpacing property is an attribute for string. So it seems like this is the reason why CharaterSpacing doesn't take effects. You will find controls with Text property such as TextBox,AutoSuggestBox and TextBlock can take effects on CharaterSpacing property since the type of Text 属性就是string.

How to apply CharacterSpacing to PlaceholderText

这里如果想把CharaterSpacing应用到PlaceholderText,可以用TextBox控件代替ContentControl,对PlaceholderTextContentPresenter做一个新的不会影响 PlaceholderText 功能的样式。但是你需要对外观做一些改变,让它看起来和以前一样。更新新 TextBox 样式的代码如下:

  <TextBox
      x:Name="PlaceholderTextContentPresenter"
      Grid.Row="1"
      Grid.ColumnSpan="2"
      Margin="{TemplateBinding BorderThickness}"
      Padding="{TemplateBinding Padding}"
      Foreground="Gray"
      CharacterSpacing="1000"
      IsHitTestVisible="False"
      BorderThickness="0"
      Text="{TemplateBinding PlaceholderText}" />

结果: