Annoying auto scroll of partially displayed items

2019-02-22 02:32发布

问题:

I have a ListView in WPF, my problem is if an item is partially displayed and I click on item, the list will automatically scroll so the whole item will be visible.

How can I disable this Auto Scroll feature?

Thank you

回答1:

had the same problem and i've found a sulotion :)

in the Xaml you define a style for the ListViewItem with this EventSetter:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
         <EventSetter Event="RequestBringIntoView" Handler="ProjectListView_OnRequestBringIntoView"/>
    </Style>
</ListView.ItemContainerStyle>

in code behind:

private void ProjectListView_OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
    e.Handled = true;
}

Hope this will work for you too :)



回答2:

The default behavior of ListView is call BringIntoView() after it was clicked. You can add an event handler to PreviewMouseDown event and handle it by set e.Handled = true;.



回答3:

I found a work around, my ListView is displaying Photo items in MVVM

private void lv_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        var p=e.OriginalSource as FrameworkElement;
        if (p != null && p.DataContext is **Photo**)
        {
            lv.SelectedItem = p.DataContext;
            e.Handled = true;
        }
    }


回答4:

Following Ekk's answer, you have to handle the bringIntoView event NOT on the parent listView but in the children event. In my peculiar case for example my listBox has some runtime-code create elements. Those can be basically stackpanel of textboxes or, at the end a stackpanel of buttons.

Despite the fact of how those are made I just have have added

btnOk.RequestBringIntoView += (s,e) => { e.Handled = true; };

to each of those and the effect was that, after clicking, they were not brought into view but the action associated with each of them was immediately executed.



回答5:

Setting ScrollViewer.VerticalScrollBarVisibility="Auto" for the list view helped.