简体   繁体   English

如何为TextBox定位装饰?

[英]How to position adorner for TextBox?

I create adorner containing TextBlock. 我创建了包含TextBlock的装饰器。 This TextBlock overlaps adorned TextBox. 此TextBlock与装饰的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? 如何对齐TextBlock以实现文本位置的匹配?

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. 除了明显调整装饰层中TextBlock的边距之外,还有另一种可能的方法。 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. 在不知道你的真正目标是什么的情况下(除了在TextBox上定位文本之外)很难说这是否会有所帮助,但是这里有。

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). 您可以使用VisualBrush ,它包含您在adorner图层中使用的TextBlock(此技术不会使用adorner图层)。 Then apply that VisualBrush to the background of the TextBox. 然后将VisualBrush应用于TextBox的背景。 This will position the text exactly in your TextBox. 这会将文本准确定位在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). 当然,当TextBox为空时,这种技术最有用,因此只有VisualBrush的TextBlock中的文本是可见的(而不是TextBox中的任何其他文本也可见)。

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). 我已经使用这种技术在TextBox为空时显示“提示文本”(此处显示的可能是ErrorTemplate和提示文本的可疑组合,但您明白了)。 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: “0到159”文本是VisualBrush中的TextBlock,当Text的属性为空时,它将应用于TextBox的背景:

使用VisualBrush显示的提示文本

Perhaps with more information about your goals we can suggest other approaches as well. 也许有关于您的目标的更多信息,我们也可以建议其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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