【问题标题】:ListView ComputedVerticalScrollBarVisibilityProperty always returns Visible?ListView ComputedVerticalScrollBarVisibilityProperty 总是返回 Visible?
【发布时间】:2017-02-17 18:20:16
【问题描述】:

我在 Listview 中有一个 GridView:

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

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

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

我在这里做错了什么?

【问题讨论】:

    标签: c# .net wpf xaml mvvm


    【解决方案1】:

    因为您要查找的值在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 应用事件和属性。

    【讨论】:

    • 我使用的是 ListView 而不是 ListBox - 我还需要 GetChildOfType 方法吗?
    • 恐怕是的,因为这是您获得内部 ScrollViewer 的方式,因为它没有公开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多