gpt4 book ai didi

xaml - 使用MVVM将ScrollIntoView转换为SelectedItem(Windows Phone 8.1)

转载 作者:行者123 更新时间:2023-12-03 10:44:22 28 4
gpt4 key购买 nike

在后面的代码中,这是通过

 ActivityList.ScrollIntoView(ActivityList.SelectedItem);

当您从详细信息页面返回时,则当您从详细信息页面返回时,不必从ListView的顶部开始。

有一些示例可滚动到ListView的末尾,而不滚动到SelectedItem。

但是,如何在MVVM中完成此操作?使用Behavior SDK创建行为?如何?

最佳答案

我已经通过附加属性(行为)完成了此操作。此行为在名为DataGridExtensions的class中,如下所示:

public static readonly DependencyProperty ScrollSelectionIntoViewProperty = DependencyProperty.RegisterAttached(
"ScrollSelectionIntoView", typeof(bool), typeof(DataGridExtensions), new PropertyMetadata(false, ScrollSelectionIntoViewChanged));

private static void ScrollSelectionIntoViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = d as DataGrid;
if (dataGrid == null)
return;

if (e.NewValue is bool && (bool)e.NewValue)
dataGrid.SelectionChanged += DataGridOnSelectionChanged;
else
dataGrid.SelectionChanged -= DataGridOnSelectionChanged;
}

private static void DataGridOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems == null || e.AddedItems.Count == 0)
return;

DataGrid dataGrid = sender as DataGrid;
if (dataGrid == null)
return;

ScrollViewer scrollViewer = UIHelper.FindChildren<ScrollViewer>(dataGrid).FirstOrDefault();
if (scrollViewer != null)
{
dataGrid.ScrollIntoView(e.AddedItems[0]);
}
}

public static void SetScrollSelectionIntoView(DependencyObject element, bool value)
{
element.SetValue(ScrollSelectionIntoViewProperty, value);
}

public static bool GetScrollSelectionIntoView(DependencyObject element)
{
return (bool)element.GetValue(ScrollSelectionIntoViewProperty);
}
UIHelper.FindChildren的代码是:
public static IList<T> FindChildren<T>(DependencyObject element) where T : FrameworkElement
{
List<T> retval = new List<T>();
for (int counter = 0; counter < VisualTreeHelper.GetChildrenCount(element); counter++)
{
FrameworkElement toadd = VisualTreeHelper.GetChild(element, counter) as FrameworkElement;
if (toadd != null)
{
T correctlyTyped = toadd as T;
if (correctlyTyped != null)
{
retval.Add(correctlyTyped);
}
else
{
retval.AddRange(FindChildren<T>(toadd));
}
}
}
return retval;
}

关于xaml - 使用MVVM将ScrollIntoView转换为SelectedItem(Windows Phone 8.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022590/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com