c# - ListView ComputedVerticalScrollBarVisibilityProperty 总是返回 Visible?

标签 c# .net wpf xaml mvvm

我在 Listview 中有一个 GridView:

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

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

出于某种原因,这行代码总是返回 Visible,即使滚动条不可见也是如此: listView.GetValue(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty)

我在这里做错了什么?

最佳答案

因为您要查找的值在 ListBox 的内部 ScrollViewer 中。

你可以通过这样的方式获取它的值:

(使用 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 应用程序事件和属性.

关于c# - ListView ComputedVerticalScrollBarVisibilityProperty 总是返回 Visible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304900/

相关文章:

c# - 命名空间和子命名空间

c# - 为什么#region 会产生编译错误?

c# - 如何使用 XAML 将数据绑定(bind)到祖先元素中的属性?

c# - 调整边框大小的问题

c# - 同时检查多个对象的 bool 属性

c# - 泛型方法和谓词的问题

c# - HttpWebRequest 不发送 header

c# - 表达式查询中的动态 Lambda 表达式

.net - DLR-为什么调试信息没有显示在我的堆栈跟踪中?

c# - 可以将 Byte[] 数组写入 C# 中的文件吗?