如何将外部元素与XAML中列表视图项的中间对齐?

时间:2012-12-20 07:03:51

标签: xaml windows-8

我想做这样的事情 -

enter image description here

标注的中间应与列表视图项的中间对齐。如何找到列表视图项中间的实际坐标,以便我可以对齐除了Path元素之外的标注?

1 个答案:

答案 0 :(得分:1)

你必须做这样的事情:

为ListView的选择更改事件创建处理程序。在该句柄中输入以下代码:

// step 1. Get selected item's container (ListViewItem in case of ListView)
var dependencyObject = listView.ItemContainerGenerator.ContainerFromIndex(listView.SelectedIndex);
var listViewItem = dependencyObject as Windows.UI.Xaml.Controls.ListViewItem;

// step 2. transform items rectangle into coordinates of containing control
var transform = listViewItem.TransformToVisual(parentContainer);
var rect = transform.TransformBounds(new Rect(0, 0, itemsControl.ActualWidth, itemsControl.ActualHeight));
var center = (rect.Top + rect.Bottom) / 2;

// step 3. position callout using center value calculated above
// exact code depends on you container, ie. Grid, Canvas, etc.