This is a migrated thread and some comments may be shown as answers.

contextmenu with MVVM

1 Answer 68 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Laurensius
Top achievements
Rank 1
Laurensius asked on 05 Mar 2014, 01:01 AM
Hi, so i want to bind into contextmenu and i tried this
<telerikPrimitives:RadDataBoundListBox
                            x:Name="NearbyListBox" Visibility="Visible"
                            SelectionChanged="NearbyListBox_SelectionChanged"
                            SelectedItem="{Binding hereNearbySelected, Mode=TwoWay}"
                            ItemsSource="{Binding hereNearby}"
                            ItemAnimationMode="PlayAll"
                            ItemAnimationEnded="NearbyListBox_ItemAnimationEnded"
                            EmptyContent="{Binding}">
                        <telerikPrimitives:RadDataBoundListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,0,0,10">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
 
                                    <Rectangle Grid.Column="0" Grid.RowSpan="2" Width="10">
                                        <Rectangle.Fill>
                                            <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                                        </Rectangle.Fill>
                                    </Rectangle>
 
                                    <StackPanel Grid.Column="1" Grid.RowSpan="2">
                                        <!--<TextBlock Text="{Binding ElementName=MainListBox,
                                                        Converter={StaticResource RowIndexConverter}}"/>-->
                                        <TextBlock Text="{Binding title}" TextWrapping="Wrap"
                                           Style="{StaticResource PhoneTextLargeStyle}"/>
                                        <TextBlock Text="{Binding category.title}" TextWrapping="Wrap"
                                           Style="{StaticResource  PhoneTextAccentStyle}"/>
                                        <TextBlock Text="{Binding vicinity}" TextWrapping="Wrap"
                                           Style="{StaticResource PhoneTextSubtleStyle}" FontFamily="Portable User Interface"/>
                                    </StackPanel>
                                    <Image x:Name="nearbyTap" Source="{Binding iconPath}" Grid.Column="2" Stretch="Uniform" Width="80"
                                           Tag="{Binding id}"  Tap="nearbyTap_Tap" />
                                    <TextBlock Text="{Binding distance, StringFormat=\{0:0 m\}}"
                                       TextWrapping="Wrap" Grid.Column="2" Grid.Row="1"
                                       Style="{StaticResource PhoneTextSubtleStyle}"/>
                                </Grid>
                            </DataTemplate>
                        </telerikPrimitives:RadDataBoundListBox.ItemTemplate>
 
                        <telerikPrimitives:RadContextMenu.ContextMenu>
                            <telerikPrimitives:RadContextMenu x:Name="menu"
                                                  Opening="OnMenuOpening">
                                <telerikPrimitives:RadContextMenuItem Content="add as favorite place"
                                                                      x:Name="favNearby" Tag="{Binding id}"
                                                                      Tap="favNearby_Tap"/>
                            </telerikPrimitives:RadContextMenu>
                        </telerikPrimitives:RadContextMenu.ContextMenu>
 
                        <telerikPrimitives:RadDataBoundListBox.ItemAddedAnimation>
                            <telerikCore:RadMoveYAnimation StartY="500" EndY="0" Duration="0:0:1">
                                <telerikCore:RadMoveYAnimation.Easing>
                                    <CubicEase EasingMode="EaseIn"/>
                                </telerikCore:RadMoveYAnimation.Easing>
                            </telerikCore:RadMoveYAnimation>
                        </telerikPrimitives:RadDataBoundListBox.ItemAddedAnimation>
 
                        <telerikPrimitives:RadDataBoundListBox.EmptyContentTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding myMessage}" TextWrapping="Wrap"
                                   HorizontalAlignment="Left" VerticalAlignment="Top"
                                   Style="{StaticResource PhoneTextGroupHeaderStyle}" FontFamily="Portable User Interface"/>
                            </DataTemplate>
                        </telerikPrimitives:RadDataBoundListBox.EmptyContentTemplate>
 
                    </telerikPrimitives:RadDataBoundListBox>

private void favNearby_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
 
    string id = (string)((RadContextMenuItem)sender).Tag;
 
    string name = "";
    string category = "";
    string icon = "";
    string href = "";
 
    for (int i = 0; i < mvm.hereNearby.Count; i++)
    {
        if (mvm.hereNearby[i].id == id)
        {
            name = mvm.hereNearby[i].title;
            category = mvm.hereNearby[i].category.title;
            icon = mvm.hereNearby[i].iconPathSquare;
            href = mvm.hereNearby[i].href;
            if (mvm.savedPlaces.Where(x => x.name == name).Count() < 1 && !string.IsNullOrWhiteSpace(name))
            {
                mvm.savedPlaces.Add(new SavedPlaces { name = name, category = category, icon = icon, href = href });
                IsoStoreHelper.SaveList<SavedPlaces>("savedPlaces", "location", mvm.savedPlaces);
            }
        }
    }
 
 
}

and i just can't getting "id" that i bind into "tag" in contextmenu, it show null, so how do i can get this "id"?

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 10 Mar 2014, 03:53 PM
Hi Tony,

Can you please clarify a bit more. From what I understand you have a list of view models and you want the context menu of the list box to have an item that has it's Tag property bound to the id of the view model that corresponds to this context menu.

If so, have you considered defining your context menu inside the list box item template itself? This way, you will have direct access to your view model, since the DataContext of the list box item template is the view model itself.

Please write again if I misunderstood your scenario and you need further assistance.

Regards,
Victor
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

Tags
ContextMenu
Asked by
Laurensius
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or