■ ItemsRepeater 엘리먼트의 ItemTemplate/Layout 속성을 사용하는 방법을 보여준다. (StackLayout, UniformGridLayout 객체 사용)
▶ Bar.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 |
namespace TestProject { /// <summary> /// 바 /// </summary> public class Bar { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public double Width { get; set; } #endregion #region 최대 너비 - MaximumWidth /// <summary> /// 최대 너비 /// </summary> public int MaximumWidth { get; set; } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public double Height { get; set; } #endregion #region 최대 높이 - MaximumHeight /// <summary> /// 최대 높이 /// </summary> public double MaximumHeight { get; set; } #endregion #region 반경 - Diameter /// <summary> /// 반경 /// </summary> public double Diameter { get; set; } #endregion #region 최대 반경 - MaximumDiameter /// <summary> /// 최대 반경 /// </summary> public double MaximumDiameter { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Bar(length, maximumLength) /// <summary> /// 생성자 /// </summary> /// <param name="length">길이</param> /// <param name="maximumLength">최대 길이</param> public Bar(double length, int maximumLength) { Width = length; MaximumWidth = maximumLength; Height = length / 4; MaximumHeight = maximumLength / 4; Diameter = length / 6; MaximumDiameter = maximumLength / 6; } #endregion } } |
▶ MainWindow.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 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" Title="TestProject"> <Grid Name="grid" Margin="10"> <Grid.Resources> <DataTemplate x:Key="HorizontalBarDataTemplateKey" x:DataType="local:Bar"> <Border Width="{x:Bind MaximumWidth}" Background="{ThemeResource SystemChromeLowColor}"> <Rectangle HorizontalAlignment="Left" Width="{x:Bind Width}" Height="24" Fill="{ThemeResource SystemAccentColor}" /> </Border> </DataTemplate> <DataTemplate x:Key="VerticalBarDataTemplateKey" x:DataType="local:Bar"> <Border Height="{x:Bind MaximumHeight}" Background="{ThemeResource SystemChromeLowColor}"> <Rectangle VerticalAlignment="Top" Width="48" Height="{x:Bind Height}" Fill="{ThemeResource SystemAccentColor}" /> </Border> </DataTemplate> <DataTemplate x:Key="CircularDataTemplateKey" x:DataType="local:Bar"> <Grid> <Ellipse HorizontalAlignment="Center" VerticalAlignment="Center" Width="{x:Bind MaximumDiameter}" Height="{x:Bind MaximumDiameter}" Fill="{ThemeResource SystemChromeLowColor}" /> <Ellipse HorizontalAlignment="Center" VerticalAlignment="Center" Width="{x:Bind Diameter}" Height="{x:Bind Diameter}" Fill="{ThemeResource SystemAccentColor}" /> </Grid> </DataTemplate> <StackLayout x:Name="horizontalStackLayout" Orientation="Horizontal" Spacing="10" /> <StackLayout x:Name="verticalStackLayout" Orientation="Vertical" Spacing="10" /> <UniformGridLayout x:Name="uniformGridLayout" MinRowSpacing="10" MinColumnSpacing="10" /> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Border Grid.Column="0" BorderThickness="1" BorderBrush="Black"> <ScrollViewer Margin="10" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollMode="Auto" IsVerticalScrollChainingEnabled="False"> <ItemsRepeater Name="repeater" ItemTemplate="{StaticResource HorizontalBarDataTemplateKey}" Layout="{StaticResource verticalStackLayout}" ItemsSource="{x:Bind BarCollection}" /> </ScrollViewer> </Border> <StackPanel Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="10"> <Button Name="addItemButton" Padding="5" Content="Add Item" /> <Button Name="removeItemButton" Padding="5" Content="Remove Item" /> <RadioButtons Name="layoutRadioButtons" Header="Layout"> <RadioButton Tag="verticalStackLayout" IsChecked="True" Content="StackLayout - Vertical" /> <RadioButton Tag="horizontalStackLayout" Content="StackLayout - Horizontal" /> <RadioButton Tag="uniformGridLayout" Content="UniformGridLayout" /> </RadioButtons> </StackPanel> </Grid> </Window> |
▶ MainWindow.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 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 |
using System; using System.Collections.ObjectModel; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 바 컬렉션 /// </summary> public ObservableCollection<Bar> BarCollection; /// <summary> /// 최대 길이 /// </summary> public int MaximumLength = 425; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 난수기 /// </summary> private Random random = new Random(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); BarCollection = new ObservableCollection<Bar>(); BarCollection.Add(new Bar(300, MaximumLength)); BarCollection.Add(new Bar(25 , MaximumLength)); BarCollection.Add(new Bar(175, MaximumLength)); this.addItemButton.Click += addItemButton_Click; this.removeItemButton.Click += removeItemButton_Click; this.layoutRadioButtons.SelectionChanged += layoutRadioButtons_SelectionChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Add Item 버튼 클릭시 처리하기 - addItemButton_Click(sender, e) /// <summary> /// Add Item 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addItemButton_Click(object sender, RoutedEventArgs e) { BarCollection.Add(new Bar(this.random.Next(MaximumLength), MaximumLength)); this.removeItemButton.IsEnabled = true; } #endregion #region Remove Item 버튼 클릭시 처리하기 - removeItemButton_Click(sender, e) /// <summary> /// Remove Item 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void removeItemButton_Click(object sender, RoutedEventArgs e) { if(BarCollection.Count > 0) { BarCollection.RemoveAt(0); if(BarCollection.Count == 0) { this.removeItemButton.IsEnabled = false; } } } #endregion #region 레이아웃 버튼 리스트 선택 변경시 처리하기 - layoutRadioButtons_SelectionChanged(sender, e) /// <summary> /// 레이아웃 버튼 리스트 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void layoutRadioButtons_SelectionChanged(object sender, SelectionChangedEventArgs e) { string itemTemplateKey = string.Empty; object itemSelected = (sender as RadioButtons).SelectedItem; if(itemSelected == null) { return; } string layoutKey = ((FrameworkElement)itemSelected).Tag as string; if(layoutKey.Equals(nameof(this.verticalStackLayout))) { itemTemplateKey = "HorizontalBarDataTemplateKey"; this.repeater.MaxWidth = MaximumLength + 12; } else if(layoutKey.Equals(nameof(this.horizontalStackLayout))) { itemTemplateKey = "VerticalBarDataTemplateKey"; this.repeater.MaxWidth = 6000; } else if(layoutKey.Equals(nameof(this.uniformGridLayout))) { itemTemplateKey = "CircularDataTemplateKey"; this.repeater.MaxWidth = 540; } this.repeater.Layout = this.grid.Resources[layoutKey] as VirtualizingLayout; this.repeater.ItemTemplate = this.grid.Resources[itemTemplateKey] as DataTemplate; this.repeater.ItemsSource = BarCollection; } #endregion } } |