TheSETJ
TheSETJ

Reputation: 528

UWP - How to apply CharacterSpacing to PlaceholderText

I want to know that is there any way to apply CharacterSpacing to PlaceholderText in TextBox?

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. Also, I tried FontStretch and again no result.

Upvotes: 0

Views: 155

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

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

The PlaceholderText is presented inside a 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 property is string.

How to apply CharacterSpacing to PlaceholderText

If you want to apply CharaterSpacing to PlaceholderText here, you can use TextBox control instead of the ContentControl for PlaceholderTextContentPresenter to be a new style which will not influence the PlaceholderText feature. But you need to do some changes for appearence to let it seems same with before. Update code for the new TextBox style as follows:

  <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}" />

And the result:

enter image description here

Upvotes: 1

Related Questions