【发布时间】:2014-08-18 17:05:19
【问题描述】:
我想获得嵌入在 PivotItem 中的 TextBlock 的屏幕位置和可见性 - 例如TB2 在屏幕上的位置和可见性:
<phone:Pivot>
<phone:PivotItem Header="first">
<TextBlock x:Name="TB1" Text="Hello 1"></TextBlock>
</phone:PivotItem>
<phone:PivotItem Header="second" >
<TextBlock x:Name="TB2" Text="Hello 2"></TextBlock>
</phone:PivotItem>
<phone:PivotItem Header="three" >
<TextBlock x:Name="TB3" Text="Hello 3"></TextBlock>
</phone:PivotItem>
</phone:Pivot>
对于其他 Xaml 控件,我使用如下代码实现了这一点:
public static Rect Position(this FrameworkElement element)
{
if (element.Visibility == Visibility.Collapsed)
return Rect.Empty;
if (element.Opacity < 0.01)
return Rect.Empty;
// Obtain transform information based off root element
GeneralTransform gt = element.TransformToVisual(Application.Current.RootVisual);
// Find the four corners of the element
Point topLeft = gt.Transform(new Point(0, 0));
Point topRight = gt.Transform(new Point(element.RenderSize.Width, 0));
Point bottomLeft = gt.Transform(new Point(0, element.RenderSize.Height));
Point bottomRight = gt.Transform(new Point(element.RenderSize.Width, element.RenderSize.Height));
var left = Math.Min(Math.Min(Math.Min(topLeft.X, topRight.X), bottomLeft.X), bottomRight.X);
var top = Math.Min(Math.Min(Math.Min(topLeft.Y, topRight.Y), bottomLeft.Y), bottomRight.Y);
var position = new Rect(left, top, element.ActualWidth, element.ActualHeight);
return position;
}
但是,对于PivotItem 孩子,当PivotItem 不在屏幕上时,此计算不会提供预期的答案。相反,它提供了当PivotItem 在屏幕上滑回时项目将在屏幕上所处的位置的答案。在过渡期间,我可以看到使用上述方法正确计算了位置 - 例如在过渡期间,我会看到位置从屏幕的左/右正确滑入。
我查看了其他属性,例如 Visible 和 Opacity,看看这里是否使用了一些其他机制来隐藏屏幕外 PivotItem 或其 Parent、GrandParent 之一等 -但所有这些似乎都会产生Visible 和Opacity == 1.0 的结果。
我还尝试迭代 VisualTree 以查看是否有任何具有纯色背景的元素掩盖了“屏幕外”PivotItems - 但我在树中看不到任何元素。
这里还有其他要考虑的属性吗? PivotItem 内容在“屏幕外”时如何隐藏?如果我真的需要,我知道我可以使用Pivot 的SelectedIndex 属性来帮助PivotItem 位置计算,但我希望尽可能避免SelectedIndex - 我'如果可能的话,我更喜欢使用通用的 Xaml 和 VisualTree 方法来获取位置。
【问题讨论】:
-
我想解释(或至少是一个提示)将在
Pivot的 ControlTemplate 中。但我似乎无法在我期望的地方找到它(“System.Windows.xaml”)。你碰巧知道如何找到它吗? -
在设计器中创建 PivotItem ControlTemplate 的副本会提供 1 in - gist.github.com/slodge/ba54683498ad2e2256f4 - 也添加到 Pivot 的 ControlTemplate 的副本中的 2 in 相同的要点 - gist.github.com/slodge/ba54683498ad2e2256f4#file-2
-
那些模板非常稀疏。我猜他们在代码隐藏中做了所有的动画/状态变化?无论如何,我错了——那里没有太多线索,没有动画,甚至没有命名部分。
-
您很可能会使用此工具找到答案(提供 21 天演示)xamlspy.com
-
介意我问你想达到什么目的(为什么你需要这个信息用于屏幕外的数据透视项目)?另外,您为什么不只使用该方法中的 topLeft 点(没有所有这些 Math.Min)?
标签: silverlight xaml windows-phone-7 windows-phone-8 windows-phone