WPF无法在Listview WPF中滚动ItemsControl

时间:2018-08-10 18:30:11

标签: c# wpf

滚动条在我的内部ItemsControl中被禁用,因此项目已从ItemsControl中被截断。如何启用它?我愿意在需要时使用其他控件。

<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>

ItemsControl中至少应有4行项目,滚动条应允许您滚动并查看其余项目。这是屏幕截图:

Truncated Items and scrollbar disabled

如果需要,这是我的其余代码:

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> { "¢ìÉæî", "äïîì", "Éæô", "öôÉ" };
        }
    }
}

2 个答案:

答案 0 :(得分:3)

Height上明确设置ScrollViewer,以启用滚动条。

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

答案 1 :(得分:3)

您必须设置高度才能启用滚动条。

 <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>