■ Button 클래스를 사용해 이미지 버튼을 만드는 방법을 보여준다.
▶ Generic.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 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:local="clr-namespace:TestLibrary"> <Style TargetType="{x:Type local:ButtonControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true"> <Grid Margin="{TemplateBinding Padding}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Name="image" Grid.Column="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="16" Height="16" Stretch="Fill" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RenderOptions.BitmapScalingMode="NearestNeighbor" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Image}"> </Image> <ContentPresenter Grid.Column="1" Margin="5 0 5 0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Grid> </themes:ButtonChrome> <ControlTemplate.Triggers> <Trigger SourceName="image" Property="Source" Value="{x:Null}"> <Setter TargetName="image" Property="Visibility" Value="Collapsed" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> |
▶ ButtonControl.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 |
using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; namespace TestLibrary { /// <summary> /// 버튼 컨트롤 /// </summary> public class ButtonControl : Button { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region ImageProperty /// <summary> /// 이미지 의존 속성 입니다. /// </summary> public static readonly DependencyProperty ImageProperty; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 라우팅 이벤트 인자 /// </summary> private RoutedEventArgs routedEventArgs = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이미지 - Image /// <summary> /// 이미지 /// </summary> [Category("ButtonControl")] [Description("이미지를 가져오거나 설정합니다.")] public ImageSource Image { get { return (ImageSource)GetValue(ImageProperty); } set { SetValue(ImageProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - ButtonControl() /// <summary> /// 생성자 /// </summary> static ButtonControl() { ImageProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "Image", typeof(ImageSource), typeof(ButtonControl), null ); DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonControl), new FrameworkPropertyMetadata(typeof(ButtonControl))); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ButtonControl() /// <summary> /// 생성자 /// </summary> public ButtonControl() { this.routedEventArgs = new RoutedEventArgs(ButtonBase.ClickEvent); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 클릭 실행하기 - PerformClick() /// <summary> /// 클릭 실행하기 /// </summary> public void PerformClick() { RaiseEvent(this.routedEventArgs); } #endregion } } |