开发者

dynamically handling events for an image

开发者 https://www.devze.com 2023-02-12 00:53 出处:网络
I\'m currently working on a program where there are multiple \"icons\" dynamically added to a window. Each one of these \"icons\" (which is actually a image with a label underneath it) represents a de

I'm currently working on a program where there are multiple "icons" dynamically added to a window. Each one of these "icons" (which is actually a image with a label underneath it) represents a device in a network. When the user double clicks on an icon, it needs to open a window where the use开发者_如何学编程r can change the properties of that specific device.

I already have the window created, along with the code to dynamically add images to a window. My problem is that since each icon is dynamically added, they use the same event handler. since each icon uses the same handler, it seems impossible to have each icon open its respective device.

Here is a part of the code which is run when a user adds a new device along with the event handler it uses:

//create new device
devices.Add(new Device(ipaddress, hn, un, pw, cm, lx, ly, tp, pl, nt, dn));
images.Add(new Image());
    //create image for main window
images[images.Count - 1].Width = 50;
images[images.Count - 1].Height = 35;
images[images.Count - 1].Stretch = Stretch.Fill;
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(deviceImages[tp], UriKind.Relative);
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.EndInit();
images[images.Count - 1].Source = logo;
images[images.Count - 1].Cursor = Cursors.Hand;
images[images.Count - 1].Margin = new System.Windows.Thickness(lx-25, ly-25, 0, 0);
images[images.Count - 1].VerticalAlignment = System.Windows.VerticalAlignment.Top;
images[images.Count - 1].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
images[images.Count - 1].MouseDown += new MouseButtonEventHandler(deviceIcon_MouseDown);
ConnexMainWindow.grid1.Children.Add(images[images.Count - 1]);

public void deviceIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
    ConnexDeviceWindow deviceWindow = new ConnexDeviceWindow();
    deviceWindow.Show();
}

As you can see, I'm not currently passing the device window the device since there isn't really any way to do so from the event handler.

My question is: Is there a way to dynamically create an event handler, for each image i add, so that i can pass the function the proper device that each image represents?


One common strategy to identify the control which fired the event is to use the Tag property of the control to store some information.

I don't know what your device model looks like, but let's assume the ip-address is unique among all the devices. You can attach that to the Image control:

images[images.Count - 1].Tag = ipaddress

In your event handler you can grab that tag to find the device (assuming the devices collection is available on class-level):

public void deviceIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
    Image image = e.Source as Image;
    if (image != null)
    {
        string ipAddress = (string)image.Tag;
        Device device = devices.FirstOrDefault(d => d.IpAddress == ipAddress);
        if (device != null)
        {
            ConnexDeviceWindow deviceWindow = new ConnexDeviceWindow(device);
            deviceWindow.Show();
        }         
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号