简体   繁体   English

WPF无法在Listview WPF中滚动ItemsControl

[英]WPF Can't scroll ItemsControl inside a Listview WPF

The scrollbar is disabled on my inner ItemsControl so items are being truncated from the ItemsControl. 滚动条在我的内部ItemsControl上被禁用,因此项目已从ItemsControl中被截断。 How can I enable it? 如何启用它? I'm open to using different controls if needed. 如果需要,我愿意使用其他控件。

<Window x:Class="WpfTestBase.View.ImportView" DataContext="{Binding Import, Source={StaticResource Locator}}">
    <ListView ItemsSource="{Binding EncodingPreviews}"
              Name="lview"
              PreviewMouseWheel="lview_PreviewMouseWheel"
              Grid.IsSharedSizeScope="True">
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type model:EncodingPreview}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="A"/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition MaxHeight="50"/>
                </Grid.RowDefinitions>
                <RadioButton IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"
                                 Content="{Binding Encoding.EncodingName}"
                                 GroupName="Grp"
                                 VerticalAlignment="Center"
                                 Margin="5,10"/>
                <ScrollViewer Grid.Column="1"
                                  Margin="5,10"
                                  VerticalScrollBarVisibility="Visible"
                                  >
                    <ItemsControl Grid.Column="1"
                                      ItemsSource="{Binding FullNames, UpdateSourceTrigger=PropertyChanged}"
                                      />
                </ScrollViewer>

            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    </ListView>
</Window>

There should be at least 4 rows of items in the ItemsControl and the scrollbar should allow you to scroll and see the rest of them. ItemsControl中至少应有4行项目,并且滚动条应允许您滚动并查看其余项目。 Here's a screenshot: 这是屏幕截图:

截断的项目和滚动条已禁用

Here's the rest of my code if you need it: 如果需要,这是我的其余代码:

namespace WpfTestBase.Model
{
    public class EncodingPreview : ObservableObject
    {
        public Encoding Encoding { get; private set; }
        private ObservableCollection<string> _fullNames = new ObservableCollection<string>();
        public ObservableCollection<string> FullNames { get { return _fullNames; } set { Set(() => FullNames, ref _fullNames, value); } }

    public EncodingPreview(Encoding encoding)
    {
        Encoding = encoding;
    }

    public override string ToString()
    {
        return Encoding.EncodingName;
    }
}

} }

public class ImportViewModel : ViewModelBase
{
    private readonly ObservableCollection<EncodingPreview> _encodingPreviews = new ObservableCollection<EncodingPreview>
    {
        new EncodingPreview(Encoding.Default), //OS' current ANSI code page
        new EncodingPreview(Encoding.UTF8)
    };

    public ObservableCollection<EncodingPreview> EncodingPreviews { get { return _encodingPreviews; } }

    /// <summary>
    /// Initializes a new instance of the ImportViewModel class.
    /// </summary>
    public ImportViewModel()
    {
        foreach (var encPrev in _encodingPreviews)
        {
            encPrev.FullNames = new ObservableCollection<string> { "¢ìÉæî", "äïîì", "Éæô", "öôÉ" };
        }
    }
}

Set Height explicitly on your ScrollViewer to enable scrollbars. ScrollViewer上显式设置Height以启用滚动条。

...
<ScrollViewer Grid.Column="1"
              Margin="5,10"
              Height="30"
              VerticalScrollBarVisibility="Visible">
    <ItemsControl Grid.Column="1" ItemsSource="{Binding FullNames, UpdateSourceTrigger=PropertyChanged}" />
</ScrollViewer>
...

You have to set the height to enable the scrollbar. 您必须设置高度才能启用滚动条。

 <ScrollViewer Grid.Column="1" Margin="5,10"
                                      VerticalScrollBarVisibility="Visible >                                         
   <StackPanel Name="stackPanel1" Height="311">
      <ItemsControl Grid.Column="1"ItemsSource="{Binding FullNames, UpdateSourceTrigger=PropertyChanged}"/>                                      
   </StackPanel>
</ScrollViewer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM