none
wpf scrollviewer中嵌套listbox后,只有当鼠标在滚动条上的时候才能通过滑轮滚动 RRS feed

  • 问题

  • scrollviewer中嵌套listbox后,只有当鼠标在滚动条上的时候才能通过滑轮滚动,鼠标移动到scrollviewer中Listbox上滚动滑轮,scrollvewer内容没有跟着滚动,原因大概是焦点被listbox捕获了,当我将Listbox设置为IsHitTestVisible="False"或IsEnable="false",即可解决,但是Listbox就无法接收鼠标点击了。

    求解鼠标放在listbox上既可以滑轮滚动scrollviewer又可以点击listbox该如何做。

    <ScrollViewer Loaded="ScrollViewer_Loaded"
                          HorizontalScrollBarVisibility="Disabled"
                          VerticalScrollBarVisibility="Auto">
                    <ListBox SelectedIndex="-1" BorderThickness="0"
                        ScrollViewer.CanContentScroll="False"
                        ItemsSource="{Binding Source={StaticResource IncreaseCollectionViewSource}}"
                        ItemTemplate="{StaticResource IncreaseListBoxDataTemplate}"
                        ItemsPanel="{StaticResource IncreaseListBoxPanelTemplate}" Background="#FFE6DEDE"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    </ListBox>
    </ScrollViewer>

    2013年11月26日 1:42

答案

  • Hi skczjun,

      欢迎来到MSDN中文论坛。

      在ListBox的PreviewMouseWheel事件中做如下处理:

    private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
            {
                ScrollViewer scrollViewer = null;
                ItemsControl depObj = (ItemsControl)sender;
                scrollViewer = FindVisualChild<ScrollViewer>(depObj);
                if (scrollViewer != null)
                {
                    scrollViewer.LineRight();
                }
            }
    public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            return (T)child;
                        }
    
                        T childItem = FindVisualChild<T>(child);
                        if (childItem != null) return childItem;
                    }
                }
                return null;
            }

      具体的代码我已经上传到我的Skydriver,地址是:https://skydrive.live.com/?cid=0b0be87e30d620ca#cid=0B0BE87E30D620CA&id=B0BE87E30D620CA%21105


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2013年11月27日 1:29
    版主