【问题标题】:WPF - Is it possible to have ListBox background to scroll together with scrollbar?WPF - 是否可以让 ListBox 背景与滚动条一起滚动?
【发布时间】:2017-02-02 04:28:31
【问题描述】:

我有一个ListBox,我用平铺的ImageBrush 映射了背景。当我在ListBox 中有很多项目并向下滚动时,背景保持在同一位置。

是否可以让背景与滚动条一起滚动?如果有,怎么做?

<ListBox ItemsSource="{Binding ItemsSource}">
    <ListBox.Background>
       <ImageBrush ImageSource="pack://application:,,,/NS;Assets/background.png" TileMode="Tile" ViewPortUnits="Absolute" Viewport="0,0,400,300"/>
    </ListBox.Background>
</ListBox>

请注意,背景比ListBoxItem 大,所以我不能将ImageBrush 背景映射到ListBoxItem

【问题讨论】:

  • 在这里尝试建议:Listview with scrolling background image。虽然它是一个列表视图,但它也可能适用于列表框。
  • 谢谢,我的问题看起来一样。但是,哪个建议好呢?我没有看到接受的答案,而且@GarryVass 的建议也不存在。
  • 这个问题的唯一答案似乎可行,但它需要相当多的编码。

标签: c# wpf


【解决方案1】:

当然有更优雅的方法可以做到这一点,但一种简单的替代方法是如下:

<ScrollViewer>
    <Grid>
        <Image Source="MyBackground.png" Stretch="None" Height="{Binding ActualHeight, ElementName=ItemsContainer}" VerticalAlignment="Top" />
        <ListBox x:Name="ItemsContainer" ItemsSource="{Binding ItemsSource}" Background="Transparent" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>

@编辑 或平铺:

<ScrollViewer>
    <Grid>
        <Grid VerticalAlignment="Top" Height="{Binding ActualHeight, ElementName=ItemsContainer}">
            <Grid.Background>
                <ImageBrush ImageSource="MyBackground.png" TileMode="Tile" ViewportUnits="[...]" Viewport="[...]" />
            </Grid.Background>
        </Grid>
        <ListBox x:Name="ItemsContainer" ItemsSource="{Binding ItemsSource}" Background="Transparent" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>

结果是:

【讨论】:

  • @kurakura88 编辑答案以添加平铺版本
  • 谢谢!我的ListBox 可以调整大小,因此我用MaxHeight="{Binding ActualHeight,... 代替...Height="{Binding ActualHeight,...
猜你喜欢
  • 2013-02-24
  • 2016-02-15
  • 2021-07-02
  • 2018-04-17
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
相关资源
最近更新 更多