■ Button 클래스에서 머터리얼 디자인(Material Design)을 사용하는 방법을 보여준다.
▶ DelegateCommand.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 |
using System; using System.Windows.Input; namespace TestProject { /// <summary> /// 대리자 명령 /// </summary> public class DelegateCommand : ICommand { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 변경시 이벤트 - CanExecuteChanged /// <summary> /// 실행 가능 여부 변경시 이벤트 /// </summary> public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 실행 액션 /// </summary> private readonly Action<object> executeAction; /// <summary> /// 실행 가능 여부 함수 /// </summary> private readonly Func<object, bool> canExecuteFunction; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DelegateCommand(executeAction, canExecuteFunction) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> /// <param name="canExecuteFunction">실행 가능 여부 함수</param> public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecuteFunction) { if(executeAction is null) { throw new ArgumentNullException(nameof(executeAction)); } this.executeAction = executeAction; this.canExecuteFunction = canExecuteFunction ?? (x => true); } #endregion #region 생성자 - DelegateCommand(executeAction) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> public DelegateCommand(Action<object> executeAction) : this(executeAction, null) { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 구하기 - CanExecute(parameter) /// <summary> /// 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>실행 가능 여부</returns> public bool CanExecute(object parameter) => this.canExecuteFunction(parameter); #endregion #region 실행하기 - Execute(parameter) /// <summary> /// 실행하기 /// </summary> /// <param name="parameter">매개 변수</param> public void Execute(object parameter) => this.executeAction(parameter); #endregion #region 갱신하기 - Refresh() /// <summary> /// 갱신하기 /// </summary> public void Refresh() => CommandManager.InvalidateRequerySuggested(); #endregion } } |
▶ MainApplicaton.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <materialDesign:BundledTheme BaseTheme="Inherit" PrimaryColor="DeepPurple" SecondaryColor="Lime" ColorAdjustment="{materialDesign:ColorAdjustment}" /> <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> |
▶ 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 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" Width="800" Height="600" Title="Button 클래스 : 머터리얼 디자인(Material Design) 사용하기" Background="{DynamicResource MaterialDesignPaper}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionMiniButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionMiniSecondaryLightButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionMiniSecondaryButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionMiniSecondaryDarkButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> </StackPanel> <StackPanel Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignFloatingActionLightButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionDarkButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionSecondaryLightButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionSecondaryButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> <Button Style="{StaticResource MaterialDesignFloatingActionSecondaryDarkButton}" Margin="10"> <materialDesign:PackIcon Width="24" Height="24" Kind="Alarm" /> </Button> </StackPanel> <StackPanel Grid.Row="2" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignRaisedLightButton}" Margin="10" Width="90" Content="_LIGHT" /> <Button Style="{StaticResource MaterialDesignRaisedButton}" Margin="10" Width="90" Content="_MID" /> <Button Style="{StaticResource MaterialDesignRaisedDarkButton}" Margin="10" Width="90" Content="_DARK" /> <Button Style="{StaticResource MaterialDesignRaisedSecondaryLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignRaisedSecondaryButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignRaisedSecondaryDarkButton}" Margin="10" Width="90" Content="DARK" /> </StackPanel> <StackPanel Grid.Row="3" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignPaperLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignPaperButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignPaperDarkButton}" Margin="10" Width="90" Content="DARK" /> <Button Style="{StaticResource MaterialDesignPaperSecondaryLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignPaperSecondaryButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignPaperSecondaryDarkButton}" Margin="10" Width="90" Content="DARK" /> </StackPanel> <StackPanel Grid.Row="4" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignFlatLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignFlatButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignFlatDarkButton}" Margin="10" Width="90" Content="DARK" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryDarkButton}" Margin="10" Width="90" Content="DARK" /> </StackPanel> <StackPanel Grid.Row="5" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignOutlinedLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignOutlinedButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignOutlinedDarkButton}" Margin="10" Width="90" Content="DARK" /> <Button Style="{StaticResource MaterialDesignOutlinedSecondaryLightButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignOutlinedSecondaryButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignOutlinedSecondaryDarkButton}" Margin="10" Width="90" Content="DARK" /> </StackPanel> <StackPanel Grid.Row="6" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignFlatLightBgButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignFlatMidBgButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignFlatDarkBgButton}" Margin="10" Width="90" Content="DARK" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryLightBgButton}" Margin="10" Width="90" Content="LIGHT" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryMidBgButton}" Margin="10" Width="90" Content="MID" /> <Button Style="{StaticResource MaterialDesignFlatSecondaryDarkBgButton}" Margin="10" Width="90" Content="DARK" /> </StackPanel> <StackPanel Grid.Row="7" HorizontalAlignment="Center" Orientation="Horizontal"> <materialDesign:Badged Margin="10" Badge="3"> <Button Content="MAIL" /> </materialDesign:Badged> <materialDesign:Badged Margin="10" Badge="{materialDesign:PackIcon Heart}" BadgeColorZoneMode="SecondaryMid"> <Button Content="LOVE" /> </materialDesign:Badged> <materialDesign:Badged Margin="10" Badge="DANGER" BadgeColorZoneMode="Dark"> <Button Content="STATUS" /> </materialDesign:Badged> <materialDesign:Badged Name="countingBadge" Margin="10" BadgeColorZoneMode="PrimaryDark"> <Button Style="{StaticResource MaterialDesignRaisedLightButton}" Content="CLICK ME!" Click="countingButton_Click" /> </materialDesign:Badged> <Button Style="{StaticResource MaterialDesignRaisedLightButton}" Margin="10" Width="150" ContentStringFormat="OR CLICK ME {0}" Content="{Binding ClickCount}" Command="{Binding ClickCommand }" /> <Button Margin="10" Content="{materialDesign:PackIcon AirplaneTakeoff}" /> </StackPanel> <StackPanel Grid.Row="8" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignRaisedAccentButton}" Margin="10" Width="150" materialDesign:RippleAssist.Feedback="#dd000000" Content="CUSTOM RIPPLE" /> <Button Style="{StaticResource MaterialDesignRaisedAccentButton}" Margin="10" Width="150" materialDesign:RippleAssist.IsDisabled="True" Content="NO RIPPLE" /> <RepeatButton Width="150" Content="REPEAT" /> </StackPanel> <StackPanel Grid.Row="9" HorizontalAlignment="Center" Orientation="Horizontal"> <Button Style="{StaticResource MaterialDesignIconButton}" Margin="10"> <materialDesign:PackIcon Kind="Play" /> </Button> <Button Style="{StaticResource MaterialDesignIconForegroundButton}" Margin="10"> <materialDesign:PackIcon Kind="Play" /> </Button> <Button Style="{StaticResource MaterialDesignIconButton}" Margin="10" Background="{DynamicResource MaterialDesignTextFieldBoxBackground}"> <materialDesign:PackIcon Kind="Play" /> </Button> </StackPanel> </Grid> </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 |
using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 클릭 카운트 /// </summary> private int clickCount; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 클릭 카운트 - ClickCount /// <summary> /// 클릭 카운트 /// </summary> public int ClickCount { get => this.clickCount; private set => SetProperty(ref this.clickCount, value); } #endregion #region 클릭 명령 - ClickCommand /// <summary> /// 클릭 명령 /// </summary> public ICommand ClickCommand { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ClickCount = 0; ClickCommand = new DelegateCommand(_ => ClickCount += 1); DataContext = this; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 카운팅 버튼 클릭시 처리하기 - countingButton_Click(sender, e) /// <summary> /// 카운팅 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void countingButton_Click(object sender, RoutedEventArgs e) { if(this.countingBadge.Badge == null || Equals(this.countingBadge.Badge, string.Empty)) { this.countingBadge.Badge = 0; } int nextCount = int.Parse(this.countingBadge.Badge.ToString() ?? "0") + 1; this.countingBadge.Badge = nextCount < 21 ? (object)nextCount : null; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); #endregion #region 속성 설정하기 - SetProperty<T>(member, value, propertyName) /// <summary> /// 속성 설정하기 /// </summary> /// <typeparam name="T">타입</typeparam> /// <param name="member">멤버</param> /// <param name="value">값</param> /// <param name="propertyName">속성명</param> /// <returns>처리 결과</returns> protected virtual bool SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null) { if(EqualityComparer<T>.Default.Equals(member, value)) { return false; } member = value; FirePropertyChangedEvent(propertyName); return true; } #endregion } } |