■ ListView 엘리먼트에서 최근 항목을 맨 아래 추가하는 방법을 보여준다.
▶ Message.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 |
using System; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메시지 /// </summary> public class Message { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 텍스트 - MessageText /// <summary> /// 메시지 텍스트 /// </summary> public string MessageText { get; private set; } #endregion #region 메시지 시간 - MessageTime /// <summary> /// 메시지 시간 /// </summary> public DateTime MessageTime { get; private set; } #endregion #region 메시지 정렬 - MessageAlignment /// <summary> /// 메시지 정렬 /// </summary> public HorizontalAlignment MessageAlignment { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Message(messageText, messageTime, messageAlignment) /// <summary> /// 생성자 /// </summary> /// <param name="messageText">메시지 텍스트</param> /// <param name="messageTime">메시지 시간</param> /// <param name="messageAlignment">메시지 정렬</param> public Message(string messageText, DateTime messageTime, HorizontalAlignment messageAlignment) { MessageText = messageText; MessageTime = messageTime; MessageAlignment = messageAlignment; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"{MessageTime} {MessageText}"; } #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 |
<?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="MessageDataTemplateKey" x:DataType="local:Message"> <Grid HorizontalAlignment="{x:Bind MessageAlignment}" Margin="5" Height="Auto"> <StackPanel Width="350" MinHeight="75" Padding="10 0 0 10" Background="{ThemeResource SystemColorHighlightColor}" CornerRadius="{StaticResource ControlCornerRadius}"> <TextBlock Padding="0 10 0 0" Foreground="{ThemeResource SystemColorHighlightTextColor}" FontSize="20" Text="{x:Bind MessageText}" /> <TextBlock Padding="0 0 0 10" Foreground="{ThemeResource SystemColorHighlightTextColor}" FontSize="15" Text="{x:Bind MessageTime}" /> </StackPanel> </Grid> </DataTemplate> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="150" /> </Grid.ColumnDefinitions> <ListView Name="listView" Grid.Column="0" ItemTemplate="{StaticResource MessageDataTemplateKey}" BorderThickness="1" BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}" SelectionMode="None"> <ListView.ItemsPanel> <ItemsPanelTemplate> <ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListView.ItemContainerStyle> </ListView> <StackPanel Grid.Column="2"> <Button Name="sendMessageButton"> Send Message </Button> <Button Name="receiveMessageButton"> Receive Message </Button> </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 |
using System; using Microsoft.UI.Xaml; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 메시지 번호 /// </summary> private int messageNumber; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); AddSendMessageItem(); this.sendMessageButton.Click += sendMessageButton_Click; this.receiveMessageButton.Click += receiveMessageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Send Message 버튼 클릭시 처리하기 - sendMessageButton_Click(sender, e) /// <summary> /// Send Message 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sendMessageButton_Click(object sender, RoutedEventArgs e) { AddSendMessageItem(); } #endregion #region Receive Message 버튼 클릭시 처리하기 - receiveMessageButton_Click(sender, e) /// <summary> /// Receive Message 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void receiveMessageButton_Click(object sender, RoutedEventArgs e) { this.listView.Items.Add ( new Message ( $"Message {++this.messageNumber}", DateTime.Now, HorizontalAlignment.Left ) ); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region Send Message 항목 추가하기 - AddSendMessageItem() /// <summary> /// Send Message 항목 추가하기 /// </summary> private void AddSendMessageItem() { this.listView.Items.Add ( new Message ( $"Message {++this.messageNumber}", DateTime.Now, HorizontalAlignment.Right ) ); } #endregion } } |