■ FlowDocumentReader 클래스에서 수평/수직 ScrollBar 객체를 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; #region 수평 스크롤바 찾기 - FindHorizontalScrollBar(flowDocumentReader) /// <summary> /// 수평 스크롤바 찾기 /// </summary> /// <param name="flowDocumentReader">플로우 문서 리더</param> /// <returns>수평 스크롤바</returns> public ScrollBar FindHorizontalScrollBar(FlowDocumentReader flowDocumentReader) { return FindScrollBar(flowDocumentReader, Orientation.Horizontal); } #endregion #region 수직 스크롤바 찾기 - FindVerticalScrollBar(flowDocumentReader) /// <summary> /// 수직 스크롤바 찾기 /// </summary> /// <param name="flowDocumentReader">플로우 문서 리더</param> /// <returns>수직 스크롤바</returns> public ScrollBar FindVerticalScrollBar(FlowDocumentReader flowDocumentReader) { return FindScrollBar(flowDocumentReader, Orientation.Vertical); } #endregion #region 스크롤바 찾기 - FindScrollBar(flowDocumentReader, orientation) /// <summary> /// 스크롤바 찾기 /// </summary> /// <param name="flowDocumentReader">플로우 문서 리더</param> /// <param name="orientation">방향</param> /// <returns>스크롤바</returns> public ScrollBar FindScrollBar(FlowDocumentReader flowDocumentReader, Orientation orientation) { if(flowDocumentReader == null) { return null; } ScrollViewer scrollViewer = VisualHelper.FindDescendantByType<ScrollViewer>(flowDocumentReader); if(scrollViewer == null) { return null; } return FindDescendantByType<ScrollBar>(scrollViewer, scrollBar => scrollBar.Orientation == orientation); } #endregion #region 타입으로 자손 찾기 - FindDescendantByType(source, descendantType, specificTypeOnly) /// <summary> /// 타입으로 자손 찾기 /// </summary> /// <param name="source">소스 객체</param> /// <param name="descendantType">자손 타입</param> /// <param name="specificTypeOnly">특정 타입만 찾기 여부</param> /// <returns>자손 객체</returns> private Visual FindDescendantByType(Visual source, Type descendantType, bool specificTypeOnly) { if(source == null) { return null; } if(specificTypeOnly ? (source.GetType() == descendantType) : (source.GetType() == descendantType) || (source.GetType().IsSubclassOf(descendantType))) { return source; } Visual descendant = null; if(source is FrameworkElement) { (source as FrameworkElement).ApplyTemplate(); } for(int i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++) { Visual visual = VisualTreeHelper.GetChild(source, i) as Visual; descendant = FindDescendantByType(visual, descendantType, specificTypeOnly); if(descendant != null) { break; } } return descendant; } #endregion #region 타입으로 자손 찾기 - FindDescendantByType(source, descendantType) /// <summary> /// 타입으로 자손 찾기 /// </summary> /// <param name="source">소스 객체</param> /// <param name="descendantType">자손 타입</param> /// <returns>자손 객체</returns> private Visual FindDescendantByType(Visual source, Type descendantType) { return FindDescendantByType(source, descendantType, true); } #endregion #region 타입으로 자손 찾기 - FindDescendantByType<TDescendant>(source) /// <summary> /// 타입으로 자손 찾기 /// </summary> /// <typeparam name="TDescendant">자손 타입</typeparam> /// <param name="source">소스 객체</param> /// <returns>자손 객체</returns> private TDescendant FindDescendantByType<TDescendant>(Visual source) where TDescendant : Visual { Visual descendant = FindDescendantByType(source, typeof(TDescendant)); return (TDescendant)descendant; } #endregion #region 타입으로 자손 찾기 - FindDescendantByType<TDescendant>(source, conditionFunction) /// <summary> /// 타입으로 자손 찾기 /// </summary> /// <typeparam name="TDescendant">자손 타입</typeparam> /// <param name="source">소스 객체</param> /// <param name="conditionFunction">조건 함수</param> /// <returns>자손 객체</returns> private TDescendant FindDescendantByType<TDescendant>(DependencyObject source, Func<TDescendant, bool> conditionFunction = null) where TDescendant : DependencyObject { if(source == null) { return null; } for(int i = 0; i < VisualTreeHelper.GetChildrenCount(source); i++) { DependencyObject child = VisualTreeHelper.GetChild(source, i); if(child is TDescendant typedChild && (conditionFunction == null || conditionFunction(typedChild))) { return typedChild; } TDescendant descendant = FindDescendantByType<TDescendant>(child, conditionFunction); if(descendant != null) { return descendant; } } return null; } #endregion |