简体   繁体   English

如何在上下文菜单WP7中绑定数据

[英]How to Bind data in context menu WP7

I have a list in which there are schedule name, date and time that are visible, but i want that on the long press of a particular item in a listbox there opens a context menu in which only description and schedule name of particular item is visible. 我有一个列表,其中有可见的日程表名称,日期和时间,但是我希望在长按列表框中的某个特定项目时打开一个上下文菜单,在该菜单中仅可见该特定项的描述和日程表名称。

So my code in xaml is: first in the grid there is a listbox in which i have bound the whole list that is scheduleList ansd in the listbox.itemtemplate and inside the data templatei have binded the particular item to the textblock 所以我在xaml中的代码是:首先在网格中有一个列表框,其中我绑定了整个列表,该列表是listbox中的scheduleList ansd.itemtemplate和数据模板内部已将特定项绑定到了textblock

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox x:Name="scheduleListbox" ItemsSource="{Binding scheduleList}" Hold="scheduleListbox_Hold" Tap="scheduleListbox_Tap" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" Height="150" Width="460">
                    <TextBlock x:Name="textBlock1" Text="{Binding ScheduleName}" Foreground="WhiteSmoke" FontSize="32"/>
                    <TextBlock x:Name="textBlock2" Text="{Binding ScheduleDate}" Foreground="Red" Margin="0,10,0,0"/>
                    <StackPanel Orientation="Horizontal" Height="70" Width="460" Hold="StackPanel_Hold">
                        <TextBlock x:Name="textBlock3" Text="{Binding StartTime}" Margin="0,5,0,0"/>
                        <TextBlock x:Name="textBlock4" Text="{Binding EndTime}" Margin="50,5,0,0"/>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu x:Name="menuItem" VerticalOffset="100.0" IsZoomEnabled="True" >

                                <toolkit:MenuItem Header="Add to calender" ItemsSource="{Binding ScheduleName }"/>
                                <!--<toolkit:MenuItem Header="Description"  ItemsSource="{Binding Description}"/>-->

                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Please tell me that how to bind description and schedule name in context menu, either by code or by xaml. 请告诉我如何通过代码或xaml在上下文菜单中绑定描述和计划名称。

How to bind data in context menu either through code or through xaml? 如何通过代码或xaml在上下文菜单中绑定数据?

I create a breadcrumb context menu with binding using the code below. 我使用下面的代码创建一个带有绑定的面包屑上下文菜单。 The code you should be interested in is the toolkit:ContextMenu.ItemTemplate section where you specify the bindings. 您应该感兴趣的代码是在其中指定绑定的toolkit:ContextMenu.ItemTemplate部分。 Notice that you can also bind to a command parameter like I do with the index value. 请注意,您也可以像使用索引值一样绑定到命令参数。

The toolkit:ContextMenu.Template section is not needed. 不需要toolkit:ContextMenu.Template部分。 I added this to allow scrolling the items if there are more than will fit on the screen and also to move the menu to the bottom of the screen. 我添加了此功能,以允许滚动项目(如果屏幕上的项目超出了屏幕的容纳范围),并将菜单移动到屏幕底部。

    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu x:Name="breadCrumbContextMenu" ItemsSource="{Binding CloudViewModel.BreadCrumbMenuItems}" Opened="ContextMenu_Opened" Closed="Breadcrumb_ContextMenu_Closed">
            <toolkit:ContextMenu.Template>
                <ControlTemplate TargetType="toolkit:ContextMenu">
                    <Border Margin="0,700,0,0" BorderThickness="1" >
                        <ScrollViewer MaxHeight="700">
                            <ItemsPresenter/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </toolkit:ContextMenu.Template>
            <toolkit:ContextMenu.ItemTemplate>
                <DataTemplate>
                    <toolkit:MenuItem Click="breadcrumbMenuItem_Click" CommandParameter="{Binding Index}" Padding="0">
                        <toolkit:MenuItem.Header>
                            <StackPanel Orientation="Horizontal" Height="40">
                                <Image Source="{Binding Image}" Width="40" Height="40" />
                                <TextBlock Text="{Binding Text}" Margin="24,0,0,0" />
                            </StackPanel>
                        </toolkit:MenuItem.Header>
                    </toolkit:MenuItem>
                </DataTemplate>
            </toolkit:ContextMenu.ItemTemplate>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM