通过上下文菜单单击从文本块(在列表框中)获取数据

时间:2011-11-09 17:01:17

标签: c# xaml windows-phone-7

标题显示我有一个带有contextmenu的列表框。我试图通过上下文菜单从列表框中的条目中获取值。我目前的代码如下;

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

    }

我正在尝试从名称绑定中获取值,我还使用标记来尝试传递数据。正如你从我的点击事件中看到的那样,它非常垃圾!从我已经阅读和理解我需要使用((FrameworkElement)发送者).Tag但我不知道如何初始化它。 感谢

2 个答案:

答案 0 :(得分:0)

您是否尝试使用MenuItem对象上的相对绑定而不是ContextMenu来移动Tag属性? 使用Tag来完成任务是一种非常常见的用法。

然后表达式Tag =“{Binding。}”绑定到ListBoxItem上绑定的整个对象的标记,所以可能必须在as ListBoxItem

中更改动态转换as YourDataModelItem

答案 1 :(得分:0)

从观点来看,你必须尝试使用​​ContextMenu项目而不是整个上下文菜单。

这就是Tag = {Binding}应该来自上下文菜单,应该为每个ContextMenu项添加。

修改后的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”是列表框项目源的每个项目类型。 :)