Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hy I try to make a Custom Control with 2D Objects. The Problem is, that i didn’t get events form 2D Object. When i replace Rectangle in the following code with a Button, the event will be sent.
What does i forgot?
Many Thanks!
File RectangleButton.cs:
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!!");
        }
    }

File Generic.xaml:
<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>

File MainWindow.xaml:
<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>
Posted

1 solution

The Rectangle has no background so doesn't get hittested.

Try

<Rectangle x:Name="myRectangle" Fill="Transparent" />

I can't believe how long it took me to find that one LOL *facepalm*
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900