ContextMenu in a DataTemplate Binding Question

I have a context menu in LongListSelector. This list is created and updated at runtime.

<phone:PanoramaItem Header="{Binding Path=LocalizedResources.SavedGamesHeader, Source={StaticResource LocalizedStrings}}" Orientation="Horizontal">
            <phone:LongListSelector Margin="0,0,-22,2" ItemsSource="{Binding SavedGames}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" Margin="12,2,0,20" Width="432">
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu>
                                    <toolkit:MenuItem Header="Remove" Click="RemoveSave_OnClick"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                            <Image Margin="10,5,10,0"  Height="173" Width="248" Source="{Binding Screen}" Stretch="Fill" HorizontalAlignment="Left"></Image>
                            <StackPanel Width="311" Margin="8,5,0,0" HorizontalAlignment="Left">
                                <TextBlock Tap="Save_OnTap" Tag="{Binding SavedGame}" Text="{Binding SaveName}" TextWrapping="Wrap" Margin="10,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="White" FontWeight="Bold" FontFamily="Arial Black" HorizontalAlignment="Left" />
                                <TextBlock Text="{Binding GameName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Left" />
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <TextBlock Text="Created on:" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                                    <TextBlock Text="{Binding Created}" TextWrapping="Wrap" Margin="5,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PanoramaItem>

Here is a method that handles a click event on a menu item

private void RemoveSave_OnClick(object sender, RoutedEventArgs e)
    {
        var menuItem = (MenuItem)sender;
        var saveViewModel = menuItem.DataContext as SavesViewModel;
        EmuStorageMgr.Instance.DeleteSave(saveViewModel.SavedGame.SaveFolder);
        App.ViewModel.RescanSaves();
    }

The following method populates the SavedGames list.

public ObservableCollection<SavesViewModel> SavedGames { get; private set; }
public void RescanSaves()
    {
        SavedGames.Clear();
        var saves = EmuStorageMgr.Instance.GetSaves();
        foreach (var save in saves)
        {
            SavedGames.Add(new SavesViewModel(save));
        }
        this.IsSavesLoaded = true;
        NotifyPropertyChanged("SavedGames");
    }

So, when the SavedGames collection is first filled, it works fine, but when the collections change (delete some old elements, add new ones), I observe strange behavior. When the OnClick event is fired, I see menuItem.DataContext not for the menu item I clicked, but for some old menu items that have been deleted.

+5
source share
1 answer

I can not leave a comment for you, so I will say here:

, . . , , .

, , msdn '11. Silverlight Framework, , . XAML, datacontext. , , .

, , codeplex . , , , LLS ( ContextMenu ) SDK, .

, , , . - , , .

Update: , , . ContextMenu Unloaded . - :

    private void add_but_up(object sender, RoutedEventArgs e)
    {
        ContextMenu conmen = (sender as ContextMenu);
        conmen.ClearValue(FrameworkElement.DataContextProperty);
    }

, , , . . , ScrollTo(). , , ContextMenu.

+8

All Articles