After discussing this in the comments this should work.
Up vote 0 down vote favorite share g+ share fb share tw.
I am creating a set of images dynamically and putting them into a Stack Panel like this :- Image image = new Image(); image. Name = "image_" + iCounter; image. Height = 100; image.
Width = 100; image. Source = bitmap; image. HorizontalAlignment = System.Windows.
HorizontalAlignment. Left; image. Stretch = Stretch.
Fill; image. VerticalAlignment = VerticalAlignment. Top; //image.
MouseDown += new MouseButtonEventHandler(image_MouseDown); image. ToolTip = "Right-Click for Options"; image. ContextMenu = GetContextMenu(); Separator separator = new Separator(); separator.
Name = "separator_" + iCounter; AddImagesStackPanel.Children. Add(image); AddImagesStackPanel.Children. Add(separator); iCounter++; Then in the Context Menu I have this code :- private System.Windows.Controls.
ContextMenu GetContextMenu() { System.Windows.Controls. MenuItem mi1; System.Windows.Controls. MenuItem mi2; System.Windows.Controls.
ContextMenu _contextMenu = new System.Windows.Controls.ContextMenu(); mi1 = new System.Windows.Controls.MenuItem(); mi1. Header = "Show Normal Size"; mi1. Click += new RoutedEventHandler(ContextMenuItem1_Click); mi2 = new System.Windows.Controls.MenuItem(); mi2.
Header = "Remove image"; mi2. Click += new RoutedEventHandler(ContextMenuItem2_Click); _contextMenu.Items. Add(mi1); _contextMenu.Items.
Add(mi2); return _contextMenu; } Now I wish to get the selected item when the user right clicks on an image and I have this code :- private void ContextMenuItem2_Click(object sender, RoutedEventArgs e) { object obj = e. OriginalSource; string imageName = ((System.Windows.Controls. Image)obj).
Name; string split = imageName. Split('_'); imageUploads. RemoveAt(Convert.
ToInt32(split1)); DisplayImagesInStackPanel(imageUploads); } But obj does not contain the name of the image since its a RoutedEventArgs. Is there any way I can get the selected item in the context menu? Thanks for your help and time wpf contextmenu stackpanel link|improve this question asked Apr 20 '11 at 21:32Johann19714 50% accept rate.
Considering that you are using WPF i'd recommend you do not add images as static content but bind your controls to a collection of images instead. – H.B. Apr 20 '11 at 22:06 can you tell me exactly what you mean by binding the controls to a collection of images? You mean putting the images in a List and then binding it to the panel?
– Johann Apr 21 '11 at 7:47 I would use the most high-level class possible, i.e. You could have a list of ImageSources or URLs and create the images automatically using DataTemplating. Also you would want to use an ObservableCollection if the list changes.
– H.B. Apr 21 '11 at 10:41 yeah good suggestion but would that help in getting the actual ID-Name from the context menu? – Johann Apr 21 '11 at 10:47 What ID-Name? What do you need that for?
– H.B. Apr 21 '11 at 11:47.
After discussing this in the comments this should work: // The binding source. Private readonly ObservableCollection _imageList = new ObservableCollection(); public ObservableCollection ImageList { get { return _imageList; } } How to display this and set up the ContextMenu: What the handlers might look like: private void Image_CM_ShowNormalSize_Click(object sender, RoutedEventArgs e) { Image img = (sender as FrameworkElement). Tag as Image; img.
Width = (img. Source as BitmapImage). PixelWidth; img.
Height = (img. Source as BitmapImage). PixelHeight; } private void Image_CM_RemoveImage_Click(object sender, RoutedEventArgs e) { BitmapImage img = (sender as FrameworkElement).
Tag as BitmapImage; // If the image is removed from the bound list the respective visual elements // will automatically be removed as well. ImageList. Remove(img); }.
Cheers HB, Will try that out and let you know! – Johann Apr 21 '11 at 14:41 but the images are not being bound to the ItemsControl. The ImageList is being populated, but no response in the ItemsControl.
Do I need to bind to it? Or is it supposed to bind automatically once the list is populated? – Johann Apr 25 '11 at 10:09 Ok got it to work!
I had to add this ImageList = new ObservableCollection(); DataContext = this; to the Initialize Code. Thanks for your help HB – Johann Apr 25 '11 at 10:20.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.