提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
使用場(chǎng)景
wpf項(xiàng)目中有個(gè)datagrid列表,當(dāng)數(shù)據(jù)出現(xiàn)垂直滾動(dòng)條時(shí)拉倒最下方,現(xiàn)在當(dāng)數(shù)據(jù)刷新時(shí)需要自動(dòng)返回到數(shù)據(jù)最上方。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
上代碼
控制部分(加在需要滾動(dòng)的地方)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-560248.html
//控制滾動(dòng)條的位置到最上方
ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(你的控件名字);
if (scrollViewer != null)
{
scrollViewer.ScrollToTop();
}
獲取VisualChild(需要添加 using System.Windows.Media;)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-560248.html
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
到了這里,關(guān)于wpf控制垂直滾動(dòng)條到最上方的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!