我需要从代码隐藏访问列表视图的滚动查看器。
这是我的列表视图的定义
<ListView Grid.Row="1" ItemsSource="{Binding Path=SpecList, UpdateSourceTrigger=PropertyChanged}"
Name="mylistview"
ItemTemplate="{StaticResource SpecElementTemplate}"
Background="{StaticResource EnvLayout}"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ItemContainerStyle="{StaticResource MyStyle}"
BorderBrush="Blue"
BorderThickness="20"
Margin="-2">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
我怎样才能获得滚动查看器?
谢谢
Andrea
有几种方法可以获得ScrollViewer
。最简单的解决方案是获取第一个孩子的第一个孩子ListView
。这意味着获得Border
和ScrollViewer
在此边界内,如中所述这个答案 https://social.msdn.microsoft.com/Forums/vstudio/en-US/794815aa-9a1e-4b57-b097-71d8672f331b/getting-the-scrollviewer-from-listview?forum=wpf:
// Get the border of the listview (first child of a listview)
Decorator border = VisualTreeHelper.GetChild(mylistview, 0) as Decorator;
// Get scrollviewer
ScrollViewer scrollViewer = border.Child as ScrollViewer;
第二种方法是扫描所有子递归以查找 ScrollViewer。马特·汉密尔顿 (Matt Hamilton) 的回答对此进行了描述这个问题 https://stackoverflow.com/questions/10279092/how-to-get-children-of-a-wpf-container-by-type。您可以简单地使用此函数来获取ScrollViewer
.
ScrollViewer scrollViewer = GetChildOfType<ScrollViewer>(mylistview);
第二种解决方案更加通用,如果您的模板也可以使用ListView
被编辑。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)