■ ItemsControl 클래스에서 리스트 컨트롤을 만드는 방법을 보여준다.
▶ Generic.xaml
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 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:local="clr-namespace:TestLibrary"> <Style TargetType="{x:Type local:ListControl}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" /> <Setter Property="BorderBrush" Value="#ff707070" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="KeyboardNavigation.TabNavigation" Value="Once" /> <Setter Property="Padding" Value="3" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:ListControl}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ScrollViewer x:Name="scrollViewer" Padding="{TemplateBinding Padding}" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type local:SelectorItem}"> <Setter Property="HorizontalContentAlignment" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:Selector}}, Path=HorizontalContentAlignment}" /> <Setter Property="VerticalContentAlignment" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:Selector}}, Path=VerticalContentAlignment}" /> <Setter Property="Padding" Value="4 0 0 0" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:SelectorItem}"> <Border x:Name="backgroundBorder" Margin="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <DockPanel Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}, Mode=FindAncestor}, Path=ActualWidth}" LastChildFill="True"> <BulletDecorator DockPanel.Dock="Left" Margin="2 0 0 0" VerticalAlignment="Center" SnapsToDevicePixels="True" Background="Transparent"> <BulletDecorator.Bullet> <themes:BulletChrome BorderBrush="Black" IsRound="{Binding Path=SingleSelect, RelativeSource={RelativeSource AncestorType={x:Type local:ListControl}, Mode=FindAncestor}}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}" RenderMouseOver="{TemplateBinding IsMouseOver}" /> </BulletDecorator.Bullet> </BulletDecorator> <ContentControl Margin="5 1 0 1" VerticalAlignment="Center" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" /> </DockPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="backgroundBorder" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> </Trigger> <Trigger Property="IsKeyboardFocusWithin" Value="true"> <Setter TargetName="backgroundBorder" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> |
▶ ListControl.cs
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
using System.Collections; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace TestLibrary { /// <summary> /// 리스트 컨트롤 /// </summary> public class ListControl : Selector { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 단일 선택 의존 속성 - SingleSelectProperty /// <summary> /// 단일 선택 의존 속성 /// </summary> public static readonly DependencyProperty SingleSelectProperty = DependencyProperty.Register ( "SingleSelect", typeof(bool), typeof(ListControl), new UIPropertyMetadata(false, singleSelectPropertyChangedCallback) ); #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// PREVIEW 마우스 DOWN 이벤트 핸들러 /// </summary> private MouseButtonEventHandler previewMouseDownEventHandler = null; /// <summary> /// PREVIEW 마우스 UP 이벤트 핸들러 /// </summary> private MouseButtonEventHandler previewMouseUpEventHandler = null; /// <summary> /// PREVIEW 마우스 이동시 이벤트 핸들러 /// </summary> private MouseEventHandler previewMouseMoveEventHandler = null; /// <summary> /// 드래그 시작 인덱스 /// </summary> private int dragStartIndex = -1; /// <summary> /// 드래그 종료 인덱스 /// </summary> private int dragEndIndex = -1; /// <summary> /// 스크롤 뷰어 /// </summary> private ScrollViewer scrollViewer = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 단일 선택 - SingleSelect /// <summary> /// 단일 선택 /// </summary> public bool SingleSelect { get { return (bool)GetValue(SingleSelectProperty); } set { SetValue(SingleSelectProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - CheckListBox() /// <summary> /// 생성자 /// </summary> static ListControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ListControl), new FrameworkPropertyMetadata(typeof(ListControl))); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CheckListBox() /// <summary> /// 생성자 /// </summary> public ListControl() { this.previewMouseDownEventHandler = new MouseButtonEventHandler(ItemsControl_PreviewMouseDown); this.previewMouseUpEventHandler = new MouseButtonEventHandler(ItemsControl_PreviewMouseUp ); this.previewMouseMoveEventHandler = new MouseEventHandler (ItemsControl_PreviewMouseMove); AddHandler(ItemsControl.PreviewMouseDownEvent, this.previewMouseDownEventHandler); AddHandler(ItemsControl.PreviewMouseUpEvent , this.previewMouseUpEventHandler ); AddHandler(ItemsControl.PreviewMouseMoveEvent, this.previewMouseMoveEventHandler); Loaded += Selector_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 단일 선택 속성 변경시 콜백 처리하기 - singleSelectPropertyChangedCallback(dependencyObject, e) /// <summary> /// 단일 선택 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void singleSelectPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { ListControl checkListBox = d as ListControl; bool newValue = (bool)e.NewValue; if(newValue) { checkListBox.surpressItemSelectionChanged = true; IList selectedList = checkListBox.SelectedItems; if(selectedList != null) { if(selectedList.Count > 1) { IList sourceList = (checkListBox.ItemsSource ?? checkListBox.Items) as IList; for(int i = 0; i < sourceList.Count; i++) { object item = sourceList[i]; SelectorItem selectorItem = checkListBox.ItemContainerGenerator.ContainerFromItem(item) as SelectorItem; selectorItem.IsSelected = false; } } } checkListBox.surpressItemSelectionChanged = false; ItemSelectionChangedEventArgs itemSelectionChangedEventArgs = new ItemSelectionChangedEventArgs ( Selector.ItemSelectionChangedEvent, checkListBox, null, true ); checkListBox.RaiseEvent(itemSelectionChangedEventArgs); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region 셀렉터 로드시 처리하기 - Selector_Loaded(sender, e) /// <summary> /// 셀렉터 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Selector_Loaded(object sender, RoutedEventArgs e) { this.scrollViewer = Template.FindName("scrollViewer", this) as ScrollViewer; } #endregion #region 항목들 컨트롤 PREVIEW 마우스 DOWN 처리하기 - ItemsControl_PreviewMouseDown(sender, e) /// <summary> /// 항목들 컨트롤 PREVIEW 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ItemsControl_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if(e.ChangedButton == MouseButton.Left) { if(Keyboard.Modifiers == ModifierKeys.Control || Keyboard.Modifiers == ModifierKeys.Shift) { return; } IList sourceList = (ItemsSource ?? Items) as IList; if(sourceList == null || sourceList.Count == 0) { return; } HitTestResult hitTestResult = VisualTreeHelper.HitTest(this, e.GetPosition(this)); if(hitTestResult == null) { return; } SelectorItem selectorItem = hitTestResult.VisualHit.FindParent<SelectorItem>(); if(selectorItem == null) { return; } object item = selectorItem.Content as object; if(item == null) { return; } this.dragStartIndex = sourceList.IndexOf(item); } } #endregion #region 항목들 컨트롤 PREVIEW 마우스 이동시 처리하기 - ItemsControl_PreviewMouseMove(sender, e) /// <summary> /// 항목들 컨트롤 PREVIEW 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ItemsControl_PreviewMouseMove(object sender, MouseEventArgs e) { if(e.LeftButton == MouseButtonState.Pressed) { Point mousePosition = e.GetPosition(this); if(this.scrollViewer != null) { if(mousePosition.Y > Height - 8) { this.scrollViewer.ScrollToVerticalOffset(this.scrollViewer.VerticalOffset + 4); } else if(mousePosition.Y < 8) { this.scrollViewer.ScrollToVerticalOffset(this.scrollViewer.VerticalOffset - 4); } } } } #endregion #region 항목들 컨트롤 PREVIEW 마우스 UP 처리하기 - ItemsControl_PreviewMouseUp(sender, e) /// <summary> /// 항목들 컨트롤 PREVIEW 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void ItemsControl_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if(e.ChangedButton == MouseButton.Left) { if(Keyboard.Modifiers == ModifierKeys.Control || Keyboard.Modifiers == ModifierKeys.Shift) { return; } IList sourceList = (ItemsSource ?? Items) as IList; if(sourceList == null || sourceList.Count == 0) { return; } HitTestResult hitTestResult = VisualTreeHelper.HitTest(this, e.GetPosition(this)); if(hitTestResult == null) { return; } SelectorItem selectorItemHitted = hitTestResult.VisualHit.FindParent<SelectorItem>(); if(selectorItemHitted == null) { return; } object itemHitted = selectorItemHitted.Content as object; if(itemHitted == null) { return; } this.dragEndIndex = sourceList.IndexOf(itemHitted); if(this.dragStartIndex == -1 || this.dragEndIndex == -1) { return; } if(SingleSelect) { this.dragStartIndex = this.dragEndIndex; } else { if(this.dragStartIndex > this.dragEndIndex) { int temporaryIndex = this.dragStartIndex; this.dragStartIndex = this.dragEndIndex; this.dragEndIndex = temporaryIndex; } } this.surpressItemSelectionChanged = true; if(SingleSelect) { SelectorItem startSelectorItem = ItemContainerGenerator.ContainerFromIndex(this.dragStartIndex) as SelectorItem; startSelectorItem.IsSelected = !startSelectorItem.IsSelected; if(startSelectorItem.IsSelected) { for(int i = 0; i < sourceList.Count; i++) { SelectorItem selectorItem = ItemContainerGenerator.ContainerFromIndex(i) as SelectorItem; if(selectorItem == startSelectorItem) { continue; } selectorItem.IsSelected = false; } } } else { for(int i = this.dragStartIndex; i < this.dragEndIndex + 1; i++) { SelectorItem selectorItem = ItemContainerGenerator.ContainerFromIndex(i) as SelectorItem; selectorItem.IsSelected = !selectorItem.IsSelected; } } this.surpressItemSelectionChanged = false; ItemSelectionChangedEventArgs itemSelectionChangedEventArgs = new ItemSelectionChangedEventArgs ( Selector.ItemSelectionChangedEvent, this, null, true ); RaiseEvent(itemSelectionChangedEventArgs); } } #endregion } } |