如何将DataTrigger绑定到单独的ListBox ScrollViewer的ComputedVerticalScrollBarVisibility属性?

时间:2012-11-29 15:57:38

标签: wpf binding listbox scrollviewer datatrigger

如果ListBox的垂直滚动条折叠,我想调整放在单独ListBox上方的边框或边距的宽度或可见性。

我正在尝试这样的事情,但无法弄清楚如何到达ListBox ScrollViewer。显然,DataTrigger中的Path不正确。

<Border Width={Binding Source={x:Static SystemParameters.ScrollWidth}}">
   <Border.Style>
      <Style>
         <Setter Property="Border.Visibility" Value="Visible"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=listBox, 
                         Path=***ScrollViewer.ComputedVerticalScrollBarVisibility***}"
                         Value="Collapsed">
               <Setter Property="Border.Visibility" Value="Collapsed"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Border.Style>
</Border>

<ListBox Name="listBox" ItemsSource="{Binding MyItems}"/>

有没有办法获得ListBox属性?如果没有,是否有更好的方法来解决这个问题?

非常感谢!

1 个答案:

答案 0 :(得分:3)

将ListBox包装在另一个ScrollViewer中的解决方法允许以牺牲冗余ScrollViewer控件为代价来使用全XAML解决方案(请参阅问题中的注释)。最后,我的队友决定使用代码隐藏解决方案,如下所示。

以下是具有我们需要访问的ScrollViewer的ListBox的相关属性。

<ListBox Name="_listBox" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         Loaded="InitializeListBoxScrollViewerProperty"/>

以下是公开滚动查看器以供外部控件使用的代码隐藏。

private static readonly DependencyPropertyKey ListBoxScrollViewerPropertyKey =
   DependencyProperty.RegisterReadOnly("ListBoxScrollViewer", typeof(ScrollViewer),
   typeof(MyEditView), new PropertyMetadata());

protected static readonly DependencyProperty ListBoxScrollViewerProperty =
   ListBoxScrollViewerPropertyKey.DependencyProperty;

protected ScrollViewer ListBoxScrollViewer
{
   get { return (ScrollViewer)GetValue(ListBoxScrollViewerProperty); }
   private set { SetValue(ListBoxScrollViewerPropertyKey, value); }
}

private void InitializeListBoxScrollViewerProperty(object sender, RoutedEventArgs args)
{
   if (ReferenceEquals(args.OriginalSource, _listBox))
   {
      var scrollViewer = _listBox.GetFirstDescendantBreadthFirst<ScrollViewer>();
      if (scrollViewer != null)
      {
         ListBoxScrollViewer = scrollViewer;
      }
   }
}

以下是使用的扩展方法:

public static class DependencyObjectExtensions
{
   public static TDescendant GetFirstDescendantBreadthFirst<TDescendant>
      (this DependencyObject dependencyObject) where TDescendant : DependencyObject
   {
      if (dependencyObject == null) { throw new ArgumentNullException(); }
      return GetFirstDescendantBreadthFirst<TDescendant>(GetAllChildren(dependencyObject));
   }

   private static TDescendant GetFirstDescendantBreadthFirst<TDescendant>
      (IEnumerable<DependencyObject> descendants) where TDescendant : DependencyObject
   {
      if (!descendants.Any()) return null;
      var descendant = descendants.OfType<TDescendant>().FirstOrDefault();
      if (descendant != null) return descendant;
      return GetFirstDescendantBreadthFirst<TDescendant>(descendants.SelectMany(GetAllChildren));
   }

   private static IEnumerable<DependencyObject> GetAllChildren(DependencyObject dependencyObject)
   {
      return Enumerable
         .Range(0, VisualTreeHelper.GetChildrenCount(dependencyObject))
         .Select(i => VisualTreeHelper.GetChild(dependencyObject, i));
   }
}

最后,可以在XAML的另一部分中访问滚动查看器。

<Grid>
   <Grid.Style>
      <Style TargetType="Grid">
         <Style.Triggers>
            <DataTrigger Binding="{Binding ListBoxScrollViewer.ComputedVerticalScrollBarVisibility,
               RelativeSource={RelativeSource AncestorType=l:MyEditView}}" Value="Visible">
               <Setter Property="Margin" Value="{StaticResource myWidenedMargin}"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Grid.Style>
</Grid>