从上下文菜单中获取所选项目

发布于 2024-11-02 07:35:57 字数 2093 浏览 5 评论 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);
    }

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

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(split[1]));
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2024-11-09 07:35:57

在评论中讨论后,这应该可以工作:

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

After discussing this in the comments this should work:

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

How to display this and set up the ContextMenu:

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

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);
}
勿挽旧人 2024-11-09 07:35:57

但是 obj 不包含图像的名称,因为它是 RoutedEventArgs。

确实如此,但是此时的 obj 是一个 MenuItem,如果向下钻取一层,就可以获得图像。

有什么方法可以在上下文菜单中获取所选项目吗?

通常情况下,人们会通过菜单的 ItemSource 绑定(或者 MenuItem 如果它们是子菜单)来加载模型类(在您的情况下为图像),并且如果有人采取这种方式他们可以将原始项目从 DataContext 中拉出来,例如在我的例子中,该项目是一个 MRU 类项目。

private void SelectMRU(object sender, RoutedEventArgs e)
{
    var mru = (e.OriginalSource as MenuItem).DataContext as MRU;
    var name = mru.Name;
    ...
}

因为您是手动执行操作,所以您应该使用相关的 Image 加载 MenuItemTag 属性,

 mi1.Tag = {Image instance in question};

然后提取事件。

 var image = (e.OriginalSource as MenuItem).Tag as Image;

But obj does not contain the name of the image since its a RoutedEventArgs.

True, but the obj at that point is a MenuItem, if you drill one level down, you can get the image.

Is there any way I can get the selected item in the context menu?

Normally one would load the model classes (Image in your case) through the binding of ItemSource of the Menu (or MenuItem if they are to be submenus) and if one takes that route they can pull the originating item off of the DataContext such as in my case the item was an MRU class item.

private void SelectMRU(object sender, RoutedEventArgs e)
{
    var mru = (e.OriginalSource as MenuItem).DataContext as MRU;
    var name = mru.Name;
    ...
}

Because you do things by hand you should load the Tag property of the MenuItem with the Image in question

 mi1.Tag = {Image instance in question};

then extract on the event.

 var image = (e.OriginalSource as MenuItem).Tag as Image;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文