public class RectangleButton : Control { private Rectangle _buttonWrite; static RectangleButton() { DefaultStyleKeyProperty.OverrideMetadata(typeof(RectangleButton), new FrameworkPropertyMetadata(typeof(RectangleButton))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); _buttonWrite = new Rectangle(); _buttonWrite = this.GetTemplateChild("myRectangle") as Rectangle; _buttonWrite.AddHandler(Rectangle.MouseDownEvent, new MouseButtonEventHandler(ButtonWrite_MouseLeftButtonDown), true); } void ButtonWrite_MouseLeftButtonDown(object sender, MouseEventArgs e) { MessageBox.Show("MouseLeftButtonDown Event!!"); } }
<resourcedictionary> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:RectangleEvent"> <Style TargetType="{x:Type local:RectangleButton}"> <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type local:RectangleButton}"> <border removed="{TemplateBinding Background}"> BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <grid> <rectangle x:name="myRectangle" xmlns:x="#unknown" /> </grid> </border> </controltemplate> </setter.value> </setter> </Style> </resourcedictionary>
<window x:class="RectangleEvent.MainWindow" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:RectangleEvent"> <grid> <my:rectanglebutton horizontalalignment="Left" x:name="rectangleButton1" verticalalignment="Top" height="141" width="162" removed="#FFF81010" xmlns:my="#unknown" /> </grid> </window>
<Rectangle x:Name="myRectangle" Fill="Transparent" />
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)