如何绑定到ListView的preferred大小? [英] How do I bind to the preferred size of a ListView?

查看:146
本文介绍了如何绑定到ListView的preferred大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF 的ListView 应该与一个始终可见页脚进行扩展。
页脚应表现得像一个头,不应该被滚动路程。
下面的XAML使用外部的ScrollViewer 链接到code背后引导ListView控件的ScrollViewer中:

I have a WPF ListView that should be extended with an always visible footer. The footer shall behave like a header and should not be scrolled away. The following XAML uses an external ScrollViewer linked to code behind to steer the ScrollViewer of the ListView:

<Window x:Class="LayoutTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="125" Width="176">
  <Grid>
    <StackPanel>
      <ListView Name="L" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
        <ListViewItem Content="Brown brownie with a preference for white wheat."/>
        <ListViewItem Content="Red Redish with a taste for oliv olives."/>
      </ListView>
      <ScrollViewer CanContentScroll="False" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ScrollChanged="ScrollViewer_ScrollChanged">
        <!-- Would like to bind Rectangle.Width to the preferred width of L -->
        <Rectangle Height="20" Width="500" Fill="Red"/>
      </ScrollViewer>
    </StackPanel>
  </Grid>
</Window>

在这背后的code是这样的:

In the code behind this looks like this:

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
  var bottomScrollViewer = sender as ScrollViewer;
  var listScrollViewer = GetScrollViewer(L) as ScrollViewer;
  if (listScrollViewer != null && bottomScrollViewer != null )
    listScrollViewer.ScrollToHorizontalOffset( bottomScrollViewer.HorizontalOffset );
}

GetScrollViewer()的定义是这样的(但不重要):

GetScrollViewer() is defined like this (but unimportant):

public static DependencyObject GetScrollViewer(DependencyObject depObj)
{
  if (depObj is ScrollViewer)
  { return depObj; }
  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  {
    var child = VisualTreeHelper.GetChild(depObj, i);
    var result = GetScrollViewer(child);
    if (result == null) { continue; }
    else { return result; }
  }
  return null;
}

的ScrollViewer 的ListView 明明知道其子女的preferred宽度。
问题是,我无法找到绑定到宽度的方法。所以在这里,它是:

The ScrollViewer of ListView obviously knows about the preferred width of its children. The problem is that I cant find a way to bind to that width. So here it is:

如何绑定 Rectangle.Width 来的preferred大小的ListView

How do I bind Rectangle.Width to the preferred size of the ListView?

或者,或者,我怎么包括在的ListView 始终可见页脚?

Or, alternatively, how do I include a footer in the ListView that is always visible?

推荐答案

您需要绑定对 ExtentWidth 你的的ScrollViewer 。据的http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.extentwidth.aspx,这是一个的DependencyProperty 。记住,你需要你的的ListView的的ScrollViewer ,不是你正在创建下面的列表视图中增加一个。

You need to bind against ExtentWidth of your ScrollViewer. According to http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.extentwidth.aspx, it's a DependencyProperty. Mind that you need the ScrollViewer of your ListView, not the additional one you are creating below the list view.

您可以使用 GetScrollViewer 函数查找的ScrollViewer 的ListView 。当然,你需要设置绑定在code-后面。这样的事情:

You can use your GetScrollViewer function to find the ScrollViewer on the ListView. Of course, you'll need to set the binding in the code-behind. Something like that:

Binding b = new Binding("ExtentWidth") { Source = GetScrollViewer(L) };
rect.SetBinding(Rectangle.WidthProperty, b);

这篇关于如何绑定到ListView的preferred大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆