WPF访问listview代码隐藏

时间:2017-03-27 17:57:13

标签: c# wpf listview code-behind scrollviewer

我需要从代码隐藏中访问listview的scrollviewer。 这是我的listview的定义

<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>

如何获得滚动查看器?

谢谢

安德烈

4 个答案:

答案 0 :(得分:4)

有几种方法可以获得ScrollViewer。最简单的解决方案是获得ListView的第一个孩子的第一个孩子。这意味着获取此边框内的BorderScrollViewer,如中所述 this answer

// 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在this question的答案中描述了这一点。您只需使用此功能即可获得ScrollViewer

ScrollViewer scrollViewer = GetChildOfType<ScrollViewer>(mylistview);

第二个解决方案更通用,如果您ListView的模板已被编辑,也会有效。

答案 1 :(得分:1)

使用VisualTreeHelper类访问任何子控件。

您的案例的Psudeo代码:

 //Declare a scroll viewer object.
 ScrollViewer  sViewer = default(ScrollViewer );

 //Start looping the child controls of your listview.
 for (int i = 0; i < VisualTreeHelper.GetChildrenCount(YOUR_LISTVIEW_OBJECT.VisualParent ); i++)
 {
        // Retrieve child visual at specified index value.
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(YOUR_LISTVIEW_OBJECT.VisualParent , i);

        ScrollViewer sViewer = childVisual as ScrollViewer;

        //You got your scroll viewer. Stop looping.
         if (sViewer != null)
         {
             break;
         }      
 }

答案 2 :(得分:0)

我不知道您为什么要访问滚动查看器,但它是附加属性和附加属性,您可以这样设置: How to I access an attached property in code behind?

答案 3 :(得分:0)

Listview 的 ScrollViewer 应该可以在 LayoutUpdated 之后访问。你可以勾住 LayoutUpdated 然后从 Visual tree

得到 if
 private static void ListView_LayoutUpdated(object sender, EventArgs e)
 {
    var listview = (ListView)sender;
    var viewer = listview.GetFirstChildOfType<ScrollViewer>();
 }

public static T GetFirstChildOfType<T>(this DependencyObject dependencyObject) where T : DependencyObject
    {
      if (dependencyObject == null)
      {
        return null;
      }

      for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
      {
        var child = VisualTreeHelper.GetChild(dependencyObject, i);

        var result = (child as T) ?? GetFirstChildOfType<T>(child);

        if (result != null)
        {
          return result;
        }
      }

      return null;
    }