Annoying auto scroll of partially displayed items

2019-02-22 02:06发布

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

5条回答
走好不送
2楼-- · 2019-02-22 02:51

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

查看更多
Lonely孤独者°
3楼-- · 2019-02-22 02:55

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.

enter image description here

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-22 03:04

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;
        }
    }
查看更多
Summer. ? 凉城
5楼-- · 2019-02-22 03:07

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

查看更多
我想做一个坏孩纸
6楼-- · 2019-02-22 03:07

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

查看更多
登录 后发表回答