2017-02-17 3 views
4

私は、リストビュー内のGridViewを持っている:ListView ComputedVerticalScrollBarVisibilityPropertyは常にVisibleを返しますか?

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

私は垂直スクロールバーがユーザーに表示されているときを検出します。何らかの理由で

は、このコード行は、常にスクロールバーが表示されていない場合でも、Visibleを返します。 listView.GetValue(ScrollViewer.ComputedVerticalScrollBarVisibilityProperty)

私はここで間違って何をしているのですか?

答えて

2

あなたが探している値はScrollViewerListBoxです。

あなたはこのようなもので、それの値を取得することができます

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

あなたが見ることができますがを使用してWPFアプリのイベントやプロパティを調べる。

+0

ListBoxではなくListBoxを使用しています - まだGetChildOfTypeメソッドが必要ですか? –

+1

これは公開されていないので、内部のScrollViewerを取得する方法であるため、恐れがあります。 – Aybe

関連する問題