標準的な Panorama ベースのアプリケーションを使用していますが、データの 1 つの重要な項目が 2 つの隣接する で繰り返されていPanoramaItems
ます。
これら2つの垂直スクロール位置を同じにしたいと思いPanoramaItems
ます(ユーザーがキーアイテムを再度見つける必要がないようにするため)。
コントロールのスクロール位置を取得して設定しPanoramaItem
、スクロールの変化を検出する方法はありますか?
一般的な解決策 (手がかりを提供してくれた Paul Diston に感謝):
/// <summary>
/// Scroll each new PanoramaItem to the same position as the previous one
/// </summary>
private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0 && e.AddedItems.Count > 0)
{
// Add logic here if only specific PanoramaItems are required to sync
ScrollViewer firstChildAsScrollViewer =
GetChildOfType(e.RemovedItems[0] as DependencyObject, typeof (ScrollViewer)) as ScrollViewer;
ScrollViewer secondChildAsScrollViewer =
GetChildOfType(e.AddedItems[0] as DependencyObject, typeof (ScrollViewer)) as ScrollViewer;
if ((firstChildAsScrollViewer != null) && (secondChildAsScrollViewer != null))
{
secondChildAsScrollViewer.ScrollToVerticalOffset(firstChildAsScrollViewer.VerticalOffset);
}
}
}
/// <summary>
/// Bredth-first recursive check for a child of the specified type
/// </summary>
private DependencyObject GetChildOfType(DependencyObject element, Type type)
{
int count = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < count; i++)
{
var e = VisualTreeHelper.GetChild(element, i);
if (e.GetType() == type)
{
return e;
}
}
// Now try the grandchildren
for (int i = 0; i < count; i++)
{
var e = VisualTreeHelper.GetChild(element, i);
var ret = GetChildOfType(e, type);
if (ret != null)
{
return ret;
}
}
return null;
}