C# ListView ComputedVerticalScrollBarVisibilityProperty是否始终返回可见?

C# ListView ComputedVerticalScrollBarVisibilityProperty是否始终返回可见?,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我在Listview中有一个GridView: <ListView> <ListView.View> <GridView> <GridViewColumn Width="100" /> <GridViewColumn Width="130" /> <GridViewColumn Wid

我在Listview中有一个GridView:

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

我想检测用户何时可以看到垂直滚动条

由于某些原因,即使滚动条不可见,这行代码始终返回可见的:
listView.GetValue(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty)


我做错了什么?

因为您要查找的值位于
列表框的内部
ScrollViewer

您可以通过以下方式获得它的价值:

(使用)

XAML:


abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
代码:

使用系统诊断;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media;
命名空间WpfApplication1
{
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
SizeChanged+=主窗口_SizeChanged;
}
私有void主窗口\u SizeChanged(对象发送方,SizeChangedEventArgs e)
{
var viewer=GetChildOfType(框);
如果(查看器!=null)
{
Debug.WriteLine(viewer.ComputedVerticalScrollBarVisibility);
}
}
公共静态T GetChildOfType(DependencyObject depObj)
其中T:DependencyObject
{
if(depObj==null)
返回null;
for(var i=0;i

您可以使用查看和研究WPF应用程序事件和属性。

我使用的是ListView而不是ListBox-我还需要GetChildOfType方法吗?恐怕是的,因为这是获取内部ScrollViewer的方式,因为它没有公开。
<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;
        }
    }
}