■ Menu 엘리먼트에서 MenuItem 엘리먼트의 스타일을 정의해서 메뉴 스타일을 설정하는 방법을 보여준다.
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.CaptionHeightKey}}" /> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}" /> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" /> <Style.Triggers> <Trigger Property="MenuItem.IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> <Setter Property="FontSize" Value="16" /> <Setter Property="FontStyle" Value="Italic" /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="_Edit"> <MenuItem Command="ApplicationCommands.Copy" InputGestureText="Ctrl+C" /> <MenuItem Command="ApplicationCommands.Cut" InputGestureText="Ctrl+X" /> <MenuItem Command="ApplicationCommands.Paste" InputGestureText="Ctrl+V" /> </MenuItem> <MenuItem Header="_Font"> <MenuItem Header="_Bold" IsCheckable="True" Checked="boldMenuItem_Checked" Unchecked="boldMenuItem_Unchecked" /> <MenuItem Header="_Italic" IsCheckable="True" Checked="italicMenuItem_Checked" Unchecked="italicMenuItem_Unchecked" /> <Separator /> <MenuItem Header="I_ncrease Font Size" Click="increaseFontMenuItem_Click" /> <MenuItem Header="_Decrease Font Size" Click="decreaseFontMenuItem_Click" /> </MenuItem> </Menu> <TextBox Name="textBox" Margin="2" TextWrapping="Wrap"> The quick brown fox jumps over the lazy dog. </TextBox> </DockPanel> </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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Bold 메뉴 항목 체크시 처리하기 - boldMenuItem_Checked(sender, e) /// <summary> /// Bold 메뉴 항목 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void boldMenuItem_Checked(object sender, RoutedEventArgs e) { this.textBox.FontWeight = FontWeights.Bold; } #endregion #region Bold 메뉴 항목 체크 취소시 처리하기 - boldMenuItem_Unchecked(sender, e) /// <summary> /// Bold 메뉴 항목 체크 취소시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void boldMenuItem_Unchecked(object sender, RoutedEventArgs e) { this.textBox.FontWeight = FontWeights.Normal; } #endregion #region Italic 메뉴 항목 체크시 처리하기 - italicMenuItem_Checked(sender, e) /// <summary> /// Italic 메뉴 항목 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void italicMenuItem_Checked(object sender, RoutedEventArgs e) { this.textBox.FontStyle = FontStyles.Italic; } #endregion #region Italic 메뉴 항목 체크 취소시 처리하기 - italicMenuItem_Unchecked(sender, e) /// <summary> /// Italic 메뉴 항목 체크 취소시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void italicMenuItem_Unchecked(object sender, RoutedEventArgs e) { this.textBox.FontStyle = FontStyles.Normal; } #endregion #region Increse Font 메뉴 항목 클릭시 처리하기 - increaseFontMenuItem_Click(sender, e) /// <summary> /// Increse Font 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void increaseFontMenuItem_Click(object sender, RoutedEventArgs e) { if(this.textBox.FontSize < 24) { this.textBox.FontSize += 2; } } #endregion #region Decrese Font 메뉴 항목 클릭시 처리하기 - decreaseFontMenuItem_Click(sender, e) /// <summary> /// Decrese Font 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void decreaseFontMenuItem_Click(object sender, RoutedEventArgs e) { if(this.textBox.FontSize > 10) { this.textBox.FontSize -= 2; } } #endregion } } |