简体   繁体   中英

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

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

How to bind data in context menu either through code or through 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. 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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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