ScrollViewer 在 WPF 中自动消失

本文关键字:消失 WPF ScrollViewer | 更新日期: 2023-09-27 17:56:03

我有一个绑定到可观察视频集合的项控件。我添加了一个垂直滚动条,但它在加载页面后消失了。

<ItemsControl x:Name="_imageList" ScrollViewer.CanContentScroll="True" HorizontalAlignment="Right" Margin="-1,0"  Width="460"  >
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="4" Rows="3"/>
                    <!--<StackPanel Orientation="Horizontal"/>-->
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                <Button Click="btn_Clicked" Margin="9,9,9,9" BorderThickness="0"  Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
                <Image x:Name="image" Source="{Binding thumbnail}"   ClipToBounds="True"/>
            </Button>
        </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

这是我页面的代码隐藏:

 public void Images()
    {
        var images = new ObservableCollection<Video>();
        var wcf = new ServiceReferenceVideo.VideoServiceClient();
        link_thumb = new Dictionary<string, string>();
        foreach (var item in wcf.GetAllVideos())
        {
            images.Add(item);
        }
        _imageList.ItemsSource = images;
    }

ScrollViewer 在 WPF 中自动消失

尝试将滚动查看器放在 ItemsControl 之外。像...

<ScrollViewer>
    <ItemsControl>
    </ItemsControl>
</ScrollViewer>

这不起作用,因为您的UniformGrid会自动采用可用大小。尝试为DataTemplate中的按钮设置固定WidthMinWidth,例如300

这是一个工作示例:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid>
    <ItemsControl x:Name="Items">
        <ItemsControl.Template>
            <ControlTemplate TargetType="ItemsControl">
                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="4"
                             IsItemsHost="True"
                             Rows="3" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button MinWidth="200"
                        MinHeight="200"
                        Margin="9"
                        Content="{Binding }" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>
</Window>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Items.ItemsSource = new List<int>(Enumerable.Range(0,100));
    }
}