ListView ComputedVerticalScrollBarVisibilityProperty总是返回Visible?

时间:2017-02-17 18:20:17

标签: c# .net wpf xaml mvvm

我在Listview中有一个GridView:

<ListView>                    
  <ListView.View>
    <GridView>
      <GridViewColumn Width="100" />               
      <GridViewColumn Width="130" />               
      <GridViewColumn Width="130" />           
    </GridView>
  </ListView.View>
</ListView>

我想检测垂直滚动条何时对用户可见。

出于某种原因,即使滚动条不可见,这行代码也始终返回VisiblelistView.GetValue(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty)

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

因为您要查找的值位于ScrollViewer内的ListBox内。

您可以使用以下内容获取它的值:

(使用How to get children of a WPF container by type?

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
    <Grid>

        <ListBox x:Name="Box">
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
            <ListBoxItem>abcd</ListBoxItem>
        </ListBox>

    </Grid>
</Window>

代码:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            SizeChanged += MainWindow_SizeChanged;
        }

        private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var viewer = GetChildOfType<ScrollViewer>(Box);
            if (viewer != null)
            {
                Debug.WriteLine(viewer.ComputedVerticalScrollBarVisibility);
            }
        }

        public static T GetChildOfType<T>(DependencyObject depObj)
            where T : DependencyObject
        {
            if (depObj == null)
                return null;

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

                var result = child as T ?? GetChildOfType<T>(child);
                if (result != null)
                    return result;
            }
            return null;
        }
    }
}

您可以使用Snoop观看和研究您的WPF应用事件和属性。