■ ComboBox 엘리먼트를 사용하는 방법을 보여준다.
▶ 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 |
<?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:contract7Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract, 7)" Title="TestProject"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <ComboBox Name="colorComboBox" HorizontalAlignment="Center" Width="200" Header="Colors" PlaceholderText="색성 선택"> <x:String>Blue</x:String> <x:String>Green</x:String> <x:String>Red</x:String> <x:String>Yellow</x:String> </ComboBox> <Rectangle Name="colorRectangle" HorizontalAlignment="Center" Margin="0 10 0 0" Width="100" Height="30" /> <ComboBox Name="fontComboBox" HorizontalAlignment="Center" Margin="0 10 0 0" Width="200" Header="폰트" DisplayMemberPath="Item1" SelectedValuePath="Item2" ItemsSource="{x:Bind FontList}" /> <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontFamily="{x:Bind (FontFamily)fontComboBox.SelectedValue, Mode=OneWay}" Text="이 텍스트에 사용되는 폰트를 설정할 수 있습니다." /> <ComboBox Name="fontSizeComboBox" HorizontalAlignment="Center" Margin="0 10 0 0" Width="200" Header="Font Size" contract7Present:IsEditable="True" ItemsSource="{x:Bind FontSizeList}" /> <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontFamily="Segoe UI" FontSize="{x:Bind (x:Double)fontSizeComboBox.SelectedValue, Mode=OneWay}" Text="이 텍스트에 사용되는 폰트 크기를 설정할 수 있습니다." /> </StackPanel> </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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
using System; using System.Collections.Generic; using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Windows.Foundation.Metadata; using Windows.UI; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 폰트 리스트 - FontList /// <summary> /// 폰트 리스트 /// </summary> public List<Tuple<string, FontFamily>> FontList { get; } = new List<Tuple<string, FontFamily>>() { new Tuple<string, FontFamily>("Arial" , new FontFamily("Arial" )), new Tuple<string, FontFamily>("Comic Sans MS" , new FontFamily("Comic Sans MS" )), new Tuple<string, FontFamily>("Courier New" , new FontFamily("Courier New" )), new Tuple<string, FontFamily>("Segoe UI" , new FontFamily("Segoe UI" )), new Tuple<string, FontFamily>("Times New Roman", new FontFamily("Times New Roman")) }; #endregion #region 폰트 크기 리스트 - FontSizeList /// <summary> /// 폰트 크기 리스트 /// </summary> public List<double> FontSizeList { get; } = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36, 48, 72 }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.colorComboBox.SelectionChanged += colorComboBox_SelectionChanged; this.fontComboBox.Loaded += fontComboBox_Loaded; this.fontSizeComboBox.Loaded += fontSizeComboBox_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 색상 콤보 박스 선택 변경시 처리하기 - colorComboBox_SelectionChanged(sender, e) /// <summary> /// 색상 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string colorName = e.AddedItems[0].ToString(); Color color; switch(colorName) { case "Yellow" : color = Colors.Yellow; break; case "Green" : color = Colors.Green; break; case "Blue" : color = Colors.Blue; break; case "Red" : color = Colors.Red; break; default : throw new Exception($"Invalid argument : {colorName}"); } colorRectangle.Fill = new SolidColorBrush(color); } #endregion #region 폰트 콤보 박스 로드시 처리하기 - fontComboBox_Loaded(sender, e) /// <summary> /// 폰트 콤보 박스 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontComboBox_Loaded(object sender, RoutedEventArgs e) { this.fontComboBox.SelectedIndex = 2; } #endregion #region 폰트 크기 콤보 박스 로드시 처리하기 - fontSizeComboBox_Loaded(sender, e) /// <summary> /// 폰트 크기 콤보 박스 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontSizeComboBox_Loaded(object sender, RoutedEventArgs e) { this.fontSizeComboBox.SelectedIndex = 2; if((ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 7))) { this.fontSizeComboBox.TextSubmitted += fontSizeComboBox_TextSubmitted; } } #endregion #region 폰트 크기 콤보 박스 텍스트 제출시 처리하기 - fontSizeComboBox_TextSubmitted(sender, e) /// <summary> /// 폰트 크기 콤보 박스 텍스트 제출시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fontSizeComboBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs e) { bool isDouble = double.TryParse(sender.Text, out double newValue); if(isDouble && (FontSizeList.Contains(newValue) || (newValue < 100 && newValue > 8))) { sender.SelectedItem = newValue; } else { sender.Text = sender.SelectedValue.ToString(); ContentDialog dialog = new ContentDialog(); dialog.Content = "폰트 크기는 8에서 100 사이의 숫자이어야 합니다."; dialog.CloseButtonText = "닫기"; dialog.DefaultButton = ContentDialogButton.Close; dialog.XamlRoot = sender.XamlRoot; _ = dialog.ShowAsync(); } e.Handled = true; } #endregion } } |