■ Control 클래스에서 ItemsRepeater 객체를 사용해 커스텀 컬렉션 컨트롤을 만드는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ ItemCollectionControl.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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; namespace TestProject; /// <summary> /// 항목 반복자 컨트롤 /// </summary> public sealed class ItemRepeaterControl : Control { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 레이아웃 속성 - LayoutProperty /// <summary> /// 레이아웃 속성 /// </summary> public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register ( nameof(Layout), typeof(Layout), typeof(ItemRepeaterControl), new PropertyMetadata(null) ); #endregion #region 항목 템플리트 속성 - ItemTemplateProperty /// <summary> /// 항목 템플리트 속성 /// </summary> public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register ( nameof(ItemTemplate), typeof(DataTemplate), typeof(ItemRepeaterControl), new PropertyMetadata(null) ); #endregion #region 소스 항목 속성 - ItemsSourceProperty /// <summary> /// 소스 항목 속성 /// </summary> public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register ( nameof(ItemsSource), typeof(object), typeof(ItemRepeaterControl), new PropertyMetadata(null) ); #endregion #region 탭 포커스 네비게이션 속성 - TabFocusNavigationProperty /// <summary> /// 탭 포커스 네비게이션 속성 /// </summary> public static readonly new DependencyProperty TabFocusNavigationProperty = DependencyProperty.Register ( nameof(TabFocusNavigation), typeof(KeyboardNavigationMode), typeof(ItemRepeaterControl), new PropertyMetadata(KeyboardNavigationMode.Local) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 레이아웃 - Layout /// <summary> /// 레이아웃 /// </summary> public Layout Layout { get { return (Layout)GetValue(LayoutProperty); } set { SetValue(LayoutProperty, value); } } #endregion #region 항목 템플리트 - ItemTemplate /// <summary> /// 항목 템플리트 /// </summary> public DataTemplate ItemTemplate { get { return (DataTemplate)GetValue(ItemTemplateProperty); } set { SetValue(ItemTemplateProperty, value); } } #endregion #region 항목 소스 - ItemsSource /// <summary> /// 항목 소스 /// </summary> public object ItemsSource { get { return (object)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } #endregion #region 탭 포커스 네비게이션 - TabFocusNavigation /// <summary> /// 탭 포커스 네비게이션 /// </summary> public new KeyboardNavigationMode TabFocusNavigation { get { return (KeyboardNavigationMode)GetValue(TabFocusNavigationProperty); } set { SetValue(TabFocusNavigationProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ItemRepeaterControl() /// <summary> /// 생성자 /// </summary> public ItemRepeaterControl() { DefaultStyleKey = typeof(ItemRepeaterControl); } #endregion } |
▶ App.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 |
<?xml version="1.0" encoding="utf-8"?> <Application x:Class="TestProject.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> </ResourceDictionary.MergedDictionaries> <Style TargetType="local:ItemRepeaterControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:ItemRepeaterControl"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"> <ScrollViewer Name="scrollViewer"> <ItemsRepeater Name="itemsRepeater" Layout="{TemplateBinding Layout}" ItemTemplate="{TemplateBinding ItemTemplate}" TabFocusNavigation="{TemplateBinding TabFocusNavigation}" ItemsSource="{TemplateBinding ItemsSource}" /> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Application.Resources> </Application> |
▶ MainPage.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 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <local:ItemRepeaterControl x:Name="itemRepeaterControl" BorderThickness="1" BorderBrush="DarkGray" Padding="10"> <local:ItemRepeaterControl.ItemTemplate> <DataTemplate x:DataType="x:String"> <TextBlock Margin="5" Text="{Binding}" /> </DataTemplate> </local:ItemRepeaterControl.ItemTemplate> <local:ItemRepeaterControl.Layout> <StackLayout Orientation="Vertical" Spacing="10" /> </local:ItemRepeaterControl.Layout> </local:ItemRepeaterControl> </Grid> </Page> |
▶ MainPage.xaml.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 |
using System.Collections.Generic; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); List<string> list = new List<string>(); for(int i = 0; i < 1000; i++) { list.Add($"항목 {i + 1}"); } this.itemRepeaterControl.ItemsSource = list; } #endregion } |