■ RichSuggestBox 엘리먼트의 ClipboardCopyFormat/ClipboardPasteFormat/DisabledFormattingAccelerators 속성을 사용해 사용자가 단순 텍스트만 입력하게 만드는 방법을 보여준다.
※ 이 예제 코드에서 사용된 RichSuggestBox 클래스는 CommunityToolkit.WinUI.Controls.RichSuggestBox 누겟(버전 : 8.0.240109)을 참조한다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ PersonType.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 |
namespace TestProject; /// <summary> /// 사람 타입 /// </summary> public class PersonType { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 초기값 - InitialValue /// <summary> /// 초기값 /// </summary> public string InitialValue => string.Empty + FirstName[0] + FamilyName[0]; #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - FamilyName /// <summary> /// 성 /// </summary> public string FamilyName { get; set; } #endregion #region 표시명 - DisplayName /// <summary> /// 표시명 /// </summary> public string DisplayName => $"{FirstName} {FamilyName}"; #endregion #region 이메일 주소 - EmailAddress /// <summary> /// 이메일 주소 /// </summary> public string EmailAddress => $"{DisplayName} <{FirstName}.{FamilyName}@contoso.com>"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return EmailAddress; } #endregion } |
▶ ItemType.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 |
using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 항목 타입 /// </summary> public class ItemType { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 - Icon /// <summary> /// 아이콘 /// </summary> public Symbol Icon { get; set; } #endregion #region 텍스트 - Text /// <summary> /// 텍스트 /// </summary> public string Text { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return Text; } #endregion } |
▶ SuggestionTemplateSelector.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 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 추천 템플리트 선택자 /// </summary> public class SuggestionTemplateSelector : DataTemplateSelector { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 사람 템플리트 - Person /// <summary> /// 사람 템플리트 /// </summary> public DataTemplate PersonTemplate { get; set; } #endregion #region 항목 템플리트 - ItemTemplate /// <summary> /// 항목 템플리트 /// </summary> public DataTemplate ItemTemplate { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 템플리트 선택하기 - SelectTemplateCore(source) /// <summary> /// 템플리트 선택하기 /// </summary> /// <param name="source">소스</param> /// <returns>템플리트</returns> protected override DataTemplate SelectTemplateCore(object source) { return source is PersonType ? this.PersonTemplate! : this.ItemTemplate!; } #endregion } |
▶ 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 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 |
<?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:control="using:CommunityToolkit.WinUI.Controls" xmlns:local="using:TestProject" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Page.Resources> <DataTemplate x:Key="PersonTemplateKey"> <StackPanel Orientation="Horizontal"> <Border Width="20" Height="20" Background="{ThemeResource AccentFillColorDefaultBrush}" CornerRadius="9999"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="10" FontWeight="Semibold" Foreground="White" Text="{Binding Initials}" /> </Border> <TextBlock Padding="10 0 0 0" Text="{Binding DisplayName}" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="ItemDataTemplateKey"> <StackPanel Orientation="Horizontal"> <SymbolIcon Symbol="{Binding Icon}" /> <TextBlock Margin="10 0 0 0" Text="{Binding Text}" /> </StackPanel> </DataTemplate> <local:SuggestionTemplateSelector x:Key="SuggestionTemplateSelectorKey" ItemTemplate="{StaticResource ItemDataTemplateKey}" PersonTemplate="{StaticResource PersonTemplateKey}" /> </Page.Resources> <StackPanel HorizontalAlignment="Center" Margin="0 50 0 0" MinWidth="400" Spacing="25"> <control:RichSuggestBox Name="richSuggestBox" HorizontalAlignment="Stretch" Header="RichSuggestBox that supports multiple prefixes (@ and #)" ItemTemplateSelector="{StaticResource SuggestionTemplateSelectorKey}" Prefixes="@#" ClipboardCopyFormat="PlainText" ClipboardPasteFormat="PlainText" DisabledFormattingAccelerators="All" /> </StackPanel> </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 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 |
using System; using System.Collections.Generic; using System.Linq; using Microsoft.UI; using Microsoft.UI.Text; using Microsoft.UI.Xaml.Controls; using CommunityToolkit.WinUI.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사람 타입 리스트 /// </summary> private readonly List<PersonType> personTypeList = new List<PersonType>() { new PersonType() { FirstName = "Marcus" , FamilyName = "Perryman" }, new PersonType() { FirstName = "Michael" , FamilyName = "Hawker" }, new PersonType() { FirstName = "Matt" , FamilyName = "Lacey" }, new PersonType() { FirstName = "Alexandre" , FamilyName = "Chohfi" }, new PersonType() { FirstName = "Filip" , FamilyName = "Wallberg" }, new PersonType() { FirstName = "Shane" , FamilyName = "Weaver" }, new PersonType() { FirstName = "Vincent" , FamilyName = "Gromfeld" }, new PersonType() { FirstName = "Sergio" , FamilyName = "Pedri" }, new PersonType() { FirstName = "Alex" , FamilyName = "Wilber" }, new PersonType() { FirstName = "Allan" , FamilyName = "Deyoung" }, new PersonType() { FirstName = "Adele" , FamilyName = "Vance" }, new PersonType() { FirstName = "Grady" , FamilyName = "Archie" }, new PersonType() { FirstName = "Megan" , FamilyName = "Bowen" }, new PersonType() { FirstName = "Ben" , FamilyName = "Walters" }, new PersonType() { FirstName = "Debra" , FamilyName = "Berger" }, new PersonType() { FirstName = "Emily" , FamilyName = "Braun" }, new PersonType() { FirstName = "Christine" , FamilyName = "Cline" }, new PersonType() { FirstName = "Enrico" , FamilyName = "Catteneo" }, new PersonType() { FirstName = "Davit" , FamilyName = "Badalyan" }, new PersonType() { FirstName = "Diego" , FamilyName = "Siciliani" }, new PersonType() { FirstName = "Raul" , FamilyName = "Razo" }, new PersonType() { FirstName = "Miriam" , FamilyName = "Graham" }, new PersonType() { FirstName = "Lynne" , FamilyName = "Robbins" }, new PersonType() { FirstName = "Lydia" , FamilyName = "Holloway" }, new PersonType() { FirstName = "Nestor" , FamilyName = "Wilke" }, new PersonType() { FirstName = "Patti" , FamilyName = "Fernandez" }, new PersonType() { FirstName = "Pradeep" , FamilyName = "Gupta" }, new PersonType() { FirstName = "Joni" , FamilyName = "Sherman" }, new PersonType() { FirstName = "Isaiah" , FamilyName = "Langer" }, new PersonType() { FirstName = "Irvin" , FamilyName = "Sayers" }, new PersonType() { FirstName = "Tung" , FamilyName = "Huynh" } }; /// <summary> /// 항목 타입 리스트 /// </summary> private readonly List<ItemType> itemTypeList = new List<ItemType>() { new ItemType() { Text = "Account" , Icon = Symbol.Account }, new ItemType() { Text = "Add Friend" , Icon = Symbol.AddFriend }, new ItemType() { Text = "Attach" , Icon = Symbol.Attach }, new ItemType() { Text = "Attach Camera", Icon = Symbol.AttachCamera }, new ItemType() { Text = "Audio" , Icon = Symbol.Audio }, new ItemType() { Text = "Block Contact", Icon = Symbol.BlockContact }, new ItemType() { Text = "Calculator" , Icon = Symbol.Calculator }, new ItemType() { Text = "Calendar" , Icon = Symbol.Calendar }, new ItemType() { Text = "Camera" , Icon = Symbol.Camera }, new ItemType() { Text = "Contact" , Icon = Symbol.Contact }, new ItemType() { Text = "Favorite" , Icon = Symbol.Favorite }, new ItemType() { Text = "Link" , Icon = Symbol.Link }, new ItemType() { Text = "Mail" , Icon = Symbol.Mail }, new ItemType() { Text = "Map" , Icon = Symbol.Map }, new ItemType() { Text = "Phone" , Icon = Symbol.Phone }, new ItemType() { Text = "Pin" , Icon = Symbol.Pin }, new ItemType() { Text = "Rotate" , Icon = Symbol.Rotate }, new ItemType() { Text = "Rotate Camera", Icon = Symbol.RotateCamera }, new ItemType() { Text = "Send" , Icon = Symbol.Send }, new ItemType() { Text = "Tags" , Icon = Symbol.Tag }, new ItemType() { Text = "UnFavorite" , Icon = Symbol.UnFavorite }, new ItemType() { Text = "UnPin" , Icon = Symbol.UnPin }, new ItemType() { Text = "Zoom" , Icon = Symbol.Zoom }, new ItemType() { Text = "ZoomIn" , Icon = Symbol.ZoomIn }, new ItemType() { Text = "ZoomOut" , Icon = Symbol.ZoomOut } }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.richSuggestBox.SuggestionRequested += richSuggestBox_SuggestionRequested; this.richSuggestBox.SuggestionChosen += richSuggestBox_SuggestionChosen; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 리치 추천 박스 추천 요청시 처리하기 - richSuggestBox_SuggestionRequested(sender, e) /// <summary> /// 리치 추천 박스 추천 요청시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richSuggestBox_SuggestionRequested(RichSuggestBox sender, SuggestionRequestedEventArgs e) { if(e.Prefix == "#") { sender.ItemsSource = this.itemTypeList.Where(x => x.Text.Contains(e.QueryText!, StringComparison.OrdinalIgnoreCase)); } else { sender.ItemsSource = this.personTypeList.Where(x => x.DisplayName.Contains(e.QueryText!, StringComparison.OrdinalIgnoreCase)); } } #endregion #region 리치 추천 박스 추천 선택시 처리하기 - richSuggestBox_SuggestionChosen(sender, e) /// <summary> /// 리치 추천 박스 추천 선택시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void richSuggestBox_SuggestionChosen(RichSuggestBox sender, SuggestionChosenEventArgs e) { if(e.Prefix == "#") { e.Format.BackgroundColor = Colors.LightSlateGray; e.Format.ForegroundColor = Colors.White; e.Format.Bold = FormatEffect.On; e.DisplayText = ((ItemType)e.SelectedItem!).Text; } else { e.DisplayText = ((PersonType)e.SelectedItem!).DisplayName; } } #endregion } |