为在列表框选择更改事件下创建的形状生成事件 [英] Generating Event for a shape created under Listbox selection changed event

查看:88
本文介绍了为在列表框选择更改事件下创建的形状生成事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个列表框和列表框中的一些图像.
现在我将图像显示在画布上,并通过鼠标按下,鼠标移动和鼠标向上事件绘制一个类似矩形的形状.并想在所有图像上复制该矩形,因此我选择了一个列表,其中包含矩形的中心点和对于每个图像,我都绘制了一个具有第一个矩形的中心点的形状.
现在,当我单击列表框中的每个图像时.旧的矩形将被隐藏,而新的矩形将以该图像的中心点创建.
好吧,这可以.

现在我想在每个图像的形状(这些形状是动态创建的)上生成鼠标单击事件

列表框选择已更改:

Hi all,

I have a list box and some images in the listbox.
Now i display the image on a canvas and draw a shape like rectangle with mouse down , mouse move and mouse up events.and want to copy the rectangle on all the images, so i have taken a list with the center point of the rectangle and for each image i have drawn a shape with the centre point of the first rectangle.
now when i click on each image in the listbox. the old rectangle will be hidden and new rectangle is created with the centre point on that image.
well up to this is OK.

Now i want to generate mouse click event on the shape(these are dynamically created) on every image

Listbox selection changed:

Rectangle rect1=new Rectangle {
                                    Stroke=Brushes .Black ,
                                    StrokeThickness=1};
rect1.Height=Myrect.Height;
rect1.Width=Myrect.Width;
     Canvas.SetLeft(rect1,Hlist.Position.X-rect1.Width/2);
     Canvas.SetTop(rect1,Hlist.Position.Y-rect1.Height/2);
Mycanvas.Children.Add(rect1);
rect1.MouseRightButtonDown+=new MouseButtonEventHandler(Rect1_Click);// this event is not raising




每次更改列表框图像选择时,都会创建一个形状.现在应该在哪里为创建的形状编写鼠标单击事件.

谢谢,




Every time when listbox image selection is changed one shape is created. now where should i write the mouse click event for the shape created.

Thanks,

推荐答案

正如我在您的代码中所看到的,您创建了新的Rectangle(rect1),该新的Rectangle(rect1)由另一个(MyRect).您将事件注册到rect1,但是添加到MyCanvasRectangleMyRect.

As I see in your code, you create new Rectangle (rect1) that is initiated with the values of another Rectangle (MyRect). You register the event to rect1 but, the Rectangle that you add to MyCanvas is MyRect.

您似乎要在画布上添加rect1而不是MyRect ...

It looks like you meant to add rect1 to the canvas instead of MyRect...


通常执行Click操作通过鼠标按钮.为什么使用MouseRightButtonDown事件?似乎您必须使用MouseLeftButtonDown事件.

The Click operation is usually performed by the left mouse button. Why do you use the MouseRightButtonDown event? It seems like you have to use the MouseLeftButtonDown event instead.


http://stackoverflow.com/questions/537073/wpf-how-do-i-create-a-textbox-dynamically-and-find-the-textbox-on-a-button-click[^]

the article may help you to solve your problem...


这篇关于为在列表框选择更改事件下创建的形状生成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆