C# 在UWP下,GetRectFromCharacterIndex不返回调整为控件样式的值
在UWP(与WPF相反)中使用C# 在UWP下,GetRectFromCharacterIndex不返回调整为控件样式的值,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,在UWP(与WPF相反)中使用GetRectFromCharacterIndex时,结果是文本输入位置的绝对偏移量,而不是控件内部 例如,以下XAML: <TextBox x:Name="noPadding" Margin="0,0,0,20" Text="aaa"/> WPF:Rect.Left==63 UWP:Rect.Left==0 如何获取控件上角色的实际位置 注意:我知道我可以通过从文本框内的文本视图计算位置来破解它。但我正在试图理解受支持的方法是什么。鉴于这还没有得到适
GetRectFromCharacterIndex
时,结果是文本输入位置的绝对偏移量,而不是控件内部
例如,以下XAML:
<TextBox x:Name="noPadding" Margin="0,0,0,20" Text="aaa"/>
WPF:Rect.Left==63
UWP:Rect.Left==0
如何获取控件上角色的实际位置
注意:我知道我可以通过从文本框内的文本视图计算位置来破解它。但我正在试图理解受支持的方法是什么。鉴于这还没有得到适当的支持,下面是我如何解决它的(如果有人找到一个“受支持”的解决方案,我将取消标记我的答案并标记该答案) 这种扩展方法似乎可以实现以下目的:
public static Rect GetRelativeRectFromCharacterIndex(this TextBox textBox, int charIndex, bool trailingEdge)
{
var caret = textBox.GetRectFromCharacterIndex(charIndex, trailingEdge);
// Hack: UWP does not properly return the location compared to the control, so we need to calculate it.
// https://stackoverflow.com/questions/50304918/under-uwp-getrectfromcharacterindex-does-not-return-values-adjusted-to-the-styl
var scroller = textBox.GetDescendants().OfType<ScrollContentPresenter>().FirstOrDefault();
var transform = scroller.TransformToVisual(textBox);
transform.TryTransform(new Point(caret.Left, caret.Top), out var topLeft);
transform.TryTransform(new Point(caret.Right, caret.Bottom), out var bottomRight);
caret = new Rect(topLeft, bottomRight);
return caret;
}
public static Rect GetRelativeRectFromCharacterIndex(此文本框文本框,int-charIndex,bool-trailinge)
{
var caret=textBox.GetRectFromCharacterIndex(charIndex,trailinge);
//Hack:UWP没有正确返回与控件相比的位置,因此我们需要计算它。
// https://stackoverflow.com/questions/50304918/under-uwp-getrectfromcharacterindex-does-not-return-values-adjusted-to-the-styl
var scroller=textBox.getsubjects().OfType().FirstOrDefault();
var transform=scroller.TransformToVisual(文本框);
transform.tryttransform(新点(插入符号左、插入符号上)、out-var-topLeft);
transform.tryttransform(新点(插入符号右、插入符号下)、out-var-bottomRight);
插入符号=新矩形(左上、右下);
返回插入符号;
}
然后,您需要getDescents():
public静态IEnumerable GetDescents(此DependencyObject容器)
{
var stack=新堆栈();
堆叠。推(容器);
而(stack.Count>0)
{
var item=stack.Pop();
变量计数=VisualTreeHelper.GetChildrenCount(项目);
for(int i=0;i
在我这边测试,结果与您相同。我们将首先通过内部渠道进行报道。目前,我担心您可能需要自己计算来实现该功能,因为没有其他TextBox
方法直接具有相同的功能。或者你可以告诉更多关于你得到这个值的原因,看看是否还有其他方法。
public static Rect GetRelativeRectFromCharacterIndex(this TextBox textBox, int charIndex, bool trailingEdge)
{
var caret = textBox.GetRectFromCharacterIndex(charIndex, trailingEdge);
// Hack: UWP does not properly return the location compared to the control, so we need to calculate it.
// https://stackoverflow.com/questions/50304918/under-uwp-getrectfromcharacterindex-does-not-return-values-adjusted-to-the-styl
var scroller = textBox.GetDescendants().OfType<ScrollContentPresenter>().FirstOrDefault();
var transform = scroller.TransformToVisual(textBox);
transform.TryTransform(new Point(caret.Left, caret.Top), out var topLeft);
transform.TryTransform(new Point(caret.Right, caret.Bottom), out var bottomRight);
caret = new Rect(topLeft, bottomRight);
return caret;
}
public static IEnumerable<DependencyObject> GetDescendants(this DependencyObject container)
{
var stack = new Stack<DependencyObject>();
stack.Push(container);
while (stack.Count > 0)
{
var item = stack.Pop();
var count = VisualTreeHelper.GetChildrenCount(item);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(item, i);
yield return child;
stack.Push(child);
}
}
}