Accessing the ScrollViewer of a ListBox from C#

前端 未结 6 1666
醉酒成梦
醉酒成梦 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 19:04

    As for me, exposing ScrollViewer as a property is a bad idea. Firstly, there is no guarantee that ScrollViewer exists in a template. Secondly, ScrollViewer works in sync with ItemsPanel and ItemContainerGenerator. Overriding this is the straight way to uncommon behavior.

    WPF controls use another pattern. Their classes are like mediators between outer logical usage and inner visual representation. Your ListBox should expose properties which can be used by ScrollViewer in a template, but not ScrollViewer. By doing this, you break WPF standards, restrict your control to specific template, and allows user code to hack internal ListBox implementation.

提交回复
热议问题