简体   繁体   中英

How to position adorner for TextBox?

I create adorner containing TextBlock. This TextBlock overlaps adorned TextBox. The problem is that starting positions of the text for these controls are not the same. How can I align TextBlock to achieve a match of text positions?

Besides the obvious of adjusting the margin of the TextBlock that is in the adorner layer, there is another possible approach that comes to mind. Without knowing what your real goal is (besides just positioning text over the TextBox) its hard to say if this would be helpful, but here goes.

You could use a VisualBrush that consists of the TextBlock that you were using in the adorner layer (this technique will not use the adorner layer). Then apply that VisualBrush to the background of the TextBox. This will position the text exactly in your TextBox. Of course, this technique is mostly useful when the TextBox is empty, so that only the text from the VisualBrush's TextBlock is visible (rather than having any additional text from the TextBox also visible).

I have used this technique for showing "hint text" when a TextBox is empty (shown here with a possibly dubious combination of an ErrorTemplate and the hint text, but you get the idea). The "0 to 159" text is a TextBlock in the VisualBrush that gets applied to the TextBox's background when its Text property is empty:

使用VisualBrush显示的提示文本

Perhaps with more information about your goals we can suggest other approaches as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM