ListView ComputedVerticalScrollBarVisibilityProperty always returns Visible?

I have a GridView in a Listview:

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

I want to determine when the vertical scrollbar is visible to the user.

For some reason, this line of code always returns Visible, even if the scroll bar is not visible: listView.GetValue(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty)

What am I doing wrong here?

+4
source share
1 answer

Because the value you are looking for is in the interior ScrollViewerin ListBox.

You can get the value of this with something like this:

(using How to get children from 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>

the code:

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

WPF Snoop.

+2

Source: https://habr.com/ru/post/1670113/


All Articles