Centering text in passwordBox in XAML

我只是一个虾纸丫 提交于 2019-12-05 02:48:11

问题


I'm trying to center the text in a PassWord box:

In Blend the Text properties for alignment are all disabled. Is there another way to do this?

Thank you for your help.


回答1:


HorizontalContentAlignment works in WPF, doesn't seem to work in Silverlight though unfortunately.

<PasswordBox Height="23" Width="400" HorizontalContentAlignment="Center" />

For Silverlight you need to tweak the template for the PasswordBox a little - you can extract the template for the password box (in Blend right click the PasswordBox, select 'Edit Template -> Edit a Copy') and then set the HorizontalAlignment on the ContentElement to {TemplateBinding HorizontalContentAlignment} - e.g.

<Border x:Name="ContentElement" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>

Then it seems to have the same behaviour as the WPF version and you can set HorizontalContentAlignment on the PasswordBox element as required.



来源:https://stackoverflow.com/questions/3366248/centering-text-in-passwordbox-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!