【问题标题】:Getting data from a textblock (on a listbox) via a contextmenu click通过上下文菜单单击从文本块(在列表框上)获取数据
【发布时间】:2011-11-10 15:33:16
【问题描述】:

正如标题所示,我有一个带有上下文菜单的列表框。我正在尝试通过上下文菜单从列表框中的条目中获取值。我目前拥有的代码如下;

<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)sender).Tag 但我不确定如何初始化它。 谢谢

【问题讨论】:

    标签: c# xaml windows-phone-7


    【解决方案1】:

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

    然后表达式 Tag="{Binding .}" 将绑定到 ListBoxItem 上的整个对象的标签绑定,因此您的动态转换 as ListBoxItem 可能必须在 as YourDataModelItem 中更改

    【讨论】:

      【解决方案2】:

      从角度来看,您必须尝试与 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”是列表框项目源的每个项目类型。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-04
        • 1970-01-01
        • 2013-05-25
        • 1970-01-01
        • 2012-12-09
        • 2022-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多