contextmenu with MVVM

2 posts, 0 answers
  1. Laurensius
    Laurensius avatar
    17 posts
    Member since:
    Jan 2014

    Posted 04 Mar 2014 Link to this post

    Hi, so i want to bind into contextmenu and i tried this
                                x:Name="NearbyListBox" Visibility="Visible"
                                SelectedItem="{Binding hereNearbySelected, Mode=TwoWay}"
                                ItemsSource="{Binding hereNearby}"
                                    <Grid Margin="0,0,0,10">
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        <Rectangle Grid.Column="0" Grid.RowSpan="2" Width="10">
                                                <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                                        <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"/>
                                        <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}"/>
                                <telerikPrimitives:RadContextMenu x:Name="menu"
                                    <telerikPrimitives:RadContextMenuItem Content="add as favorite place"
                                                                          x:Name="favNearby" Tag="{Binding id}"
                                <telerikCore:RadMoveYAnimation StartY="500" EndY="0" Duration="0:0:1">
                                        <CubicEase EasingMode="EaseIn"/>
                                    <TextBlock Text="{Binding myMessage}" TextWrapping="Wrap"
                                       HorizontalAlignment="Left" VerticalAlignment="Top"
                                       Style="{StaticResource PhoneTextGroupHeaderStyle}" FontFamily="Portable User Interface"/>

    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 => == 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"?
  2. Victor
    Victor avatar
    1349 posts

    Posted 10 Mar 2014 Link to this post

    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.


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

  3. DevCraft banner
Back to Top