在ListView的选定项目中滚动

时间:2014-01-27 13:32:17

标签: c# wpf listview scroll selecteditem

在WPF上 - ListView控制所选项目必须可视化一些其他详细信息。 如果选择了某个项目,则需要更多空间,然后ListView控件才会提供。

默认情况下,无法在所选项目内滚动。向下滚动, 它直接跳到下一个项目,无法看到所选项目的底部。

任何人都知道如何在所选项目中滚动?

以下代码演示了该行为。在实际代码中,所选项目更复杂,但举例来说,选择项目大小时只需修改它:

XAML:

<Window x:Class="ListViewWithLargeSelectedItem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="300" Width="300">
<Grid>
    <ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" HorizontalContentAlignment="Stretch">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border x:Name="border" Padding="10" HorizontalAlignment="Stretch">
                    <TextBlock Text="{Binding Text}" />
                </Border>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}"
                                             Value="true">
                        <Setter TargetName="border" Property="Padding"
                                        Value="40,200" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

代码背后:

public partial class MainWindow : Window
{
    private CustomItem _selectedItem;
    public CustomItem SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (_selectedItem != null)
            {
                _selectedItem.IsSelected = false;
            }
            _selectedItem = value;
            _selectedItem.IsSelected = true;
        }
    }

    public List<CustomItem> Items
    {
        get { return (List<CustomItem>)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(List<CustomItem>), typeof(MainWindow), new UIPropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        Items = new List<CustomItem>();
        for (int i = 0; i < 10; i++)
        {
            Items.Add(new CustomItem() { IsSelected = false, Text = "ITEM " + i });             
        }
        DataContext = this;
    }
}

public class CustomItem : INotifyPropertyChanged
{
    public string Text { get; set; }
    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (_isSelected == value)
            {
                return;
            }

            _isSelected = value;
            NotifyOfPropertyChange("IsSelected");
        }
    }

    private void NotifyOfPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
}

1 个答案:

答案 0 :(得分:1)

您的问题不是很明确,但如果您说ListView使用整个项目滚动并希望它使用像素滚动,那么请参阅MSDN上的ScrollViewer.CanContentScroll property页面。如果是这种情况,那么您只需要在False上将此附加属性设置为ListView即可启用平滑滚动:

<ListView ScrollViewer.CanContentScroll="False" ... />