English 中文(简体)
从文字栏目(清单箱)中提取数据
原标题:Getting data from a textblock (on a listbox) via a contextmenu click

如标题所示,我有一份附有背景的清单箱。 我试图从名单箱的条目中获取价值。 现行法典如下:

<ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#90361F" BorderThickness="3" CornerRadius="5" Margin="5">

                    <StackPanel Orientation="Vertical" Background="#90361F" Width="488"> 
                                <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu Tag="{Binding .}">
                            <toolkit:MenuItem Click="MenuItemDelete_Click" Header="Delete Timer"/>
                        </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <StackPanel Orientation="Vertical" Background="#90361F" Width="488" >
                            <TextBlock Text="{Binding e2name}" Foreground="White" FontSize="25"/>
                            <StackPanel Orientation="Horizontal" Margin="5">
                                <TextBlock Text="{Binding name}" Foreground="White" FontSize="15" Tag="{Binding name}"/>
                                <TextBlock Text="{Binding datetime}" Foreground="White" FontSize="15" HorizontalAlignment="Right"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>

                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

现在情况简介会点击事件

private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
    {

        var me = ((FrameworkElement)sender).Tag as ListBoxItem;

    }

i m trying to get the value from the binding of name, i m also using a tag to try and pass the data. as you can see from my click event, well it s pretty rubbish! From what i ve read and understand i do need to make use of ((FrameworkElement)sender).Tag but i m not sure on how to init it. thanks

问题回答

Did you try to move the Tag attribute with relative binding on the MenuItem object instead of ContextMenu one? Using the Tag to achieve your task is a pretty common use.

然后,“Tag=”{Bled }”对受清单BoxItem约束的整批物体的标签具有约束力,因此很可能是您的有活力的<代码>作为清单BoxItem。 页: 1 页: 1

In point of view, you have to try with binding with ContextMenu item than the whole context menu.

这就是Tag={Bled}应从Mendu的角度来看,应当添加到每个背景项目。

经修改的Xaml如下。

<ListBox Name="ListBox" Height="500" ItemsSource="{Binding List}">
<ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#90361F" BorderThickness="3" CornerRadius="5" Margin="5">

                    <StackPanel Orientation="Vertical" Background="#90361F" Width="488"> 
                                <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem Click="MenuItemDelete_Click" Header="Delete Timer" Tag="{Binding}"/>
                        </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <StackPanel Orientation="Vertical" Background="#90361F" Width="488" >
                            <TextBlock Text="{Binding e2name}" Foreground="White" FontSize="25"/>
                            <StackPanel Orientation="Horizontal" Margin="5">
                                <TextBlock Text="{Binding name}" Foreground="White" FontSize="15" Tag="{Binding name}"/>
                                <TextBlock Text="{Binding datetime}" Foreground="White" FontSize="15" HorizontalAlignment="Right"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
  </ListBox>

还确保“ListBoxItem”是每个项目的箱子项目源。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签