2011-04-20 82 views
0

我动态创建一组图像,并把它们放入一个堆叠面板这样的选择项: -获取从上下文菜单

  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++; 

然后在上下文菜单我有这样的代码: -

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; 
    } 

现在我希望得到所选择的项目,当用户在图像上用鼠标右击,我有这样的代码: -

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(split[1])); 
     DisplayImagesInStackPanel(imageUploads); 
    } 

但是,由于其RoutedEventArgs,obj不包含图像的名称。有什么方法可以在上下文菜单中获取所选项目吗?

感谢您的帮助和时间

+0

考虑到您使用的是WPF,我建议您不要将图像添加为静态内容,而是将您的控件绑定到图像集合。 – 2011-04-20 22:06:59

+0

嗨,HB,你能准确地告诉我你的意思吗?将控件绑定到一组图像上?你的意思是将图像放在列表中,然后将其绑定到面板上? – Johann 2011-04-21 07:47:02

+0

我会尽可能使用最高级别的类,也就是说,您可以拥有一个ImageSources或URL列表,并使用[DataTemplating](http://msdn.microsoft.com/zh-cn/library/ms742521)自动创建图像。 ASPX)。如果列表更改,您也会想要使用ObservableCollection 。 – 2011-04-21 10:41:02

回答

3

在评论中讨论此之后这应该工作:

// The binding source. 
private readonly ObservableCollection<BitmapImage> _imageList = new ObservableCollection<BitmapImage>(); 
public ObservableCollection<BitmapImage> ImageList 
{ 
    get { return _imageList; } 
} 

如何显示这一点,并成立了文本菜单:

<ItemsControl ItemsSource="{Binding ImageList}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Image Source="{Binding}" Width="100" Height="100" 
         HorizontalAlignment="Left" Stretch="Fill" 
         VerticalAlignment="Top" ToolTip="Right-Click for Options"> 
        <Image.ContextMenu> 
         <ContextMenu> 
          <MenuItem Header="Show Normal Size" Click="Image_CM_ShowNormalSize_Click" 
             Tag="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget}"/> <!-- The placement target is the object to which the context menu belongs, i.e. the image --> 
          <MenuItem Header="Remove Image" Click="Image_CM_RemoveImage_Click" 
             Tag="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext}"/> <!-- The DataContext of the Image is the BitmapImage, which should be removed from the list --> 
         </ContextMenu> 
        </Image.ContextMenu> 
       </Image> 
       <Separator/> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

什么处理程序可能看起来像:

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); 
} 
+0

欢呼声HB,请试试看,让你知道! – Johann 2011-04-21 14:41:20

+0

嗨HB,我试过你的代码,但图像没有被绑定到ItemsControl。 ImageList正在被填充,但ItemsControl中没有响应。我需要绑定到它吗?还是它应该自动绑定一旦列表填充? – Johann 2011-04-25 10:09:43

+0

好吧,它的工作!我不得不添加这个ImageList = new ObservableCollection (); DataContext = this; 初始化代码。感谢您的帮助HB – Johann 2011-04-25 10:20:18