Accessing the ScrollViewer of a ListBox from C#

前端 未结 6 1673
醉酒成梦
醉酒成梦 2020-12-01 18:26

I\'d like to change the properties of a ScrollViewer of a ListBox from C#.

I found this question here on Stackoverflow. I took the accepted

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 18:55

    Here's another reworked and generic version of @punker76's answer for C# 6:

    public static class VisualExtensions
    {
        public static T FindVisualDescendant(this Visual element) where T : Visual
        {
            if (element == null)
                return null;
    
            var e = element as T;
    
            if (e != null)
                return e;
    
            (element as FrameworkElement)?.ApplyTemplate();
    
            var childrenCount = VisualTreeHelper.GetChildrenCount(element);
    
            for (var i = 0; i < childrenCount; i++)
            {
                var visual = VisualTreeHelper.GetChild(element, i) as Visual;
    
                var foundElement = visual.FindVisualDescendant();
    
                if (foundElement != null)
                    return foundElement;
            }
    
            return null;
        }
    }
    

提交回复
热议问题