1

I am using PRISM to develop my Windows Phone app using the MVVM design pattern. I need to pass my SelectedItem object from my LongListSelector through my delegate command into my method.

I can do that. The problem is, I'm passing in the wrong object. I don't know if it's a design problem or I am binding improperly.

I need the object to be an Album object. What I'm getting instead is either null or my ViewModel. (I've changed the code a few times and those are the only things I can get.)

XAML

<phone:LongListSelector x:Name="AlbumList" ItemsSource="{Binding Albums}"
                 Margin="10,0,0,0" LayoutMode="Grid" GridCellSize="200, 200"
                 ItemTemplate="{StaticResource AlbumTemplate}"
                                toolkit:TiltEffect.IsTiltEnabled="True"
                                >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding DataContext.SelectAlbumCommand, ElementName=ContentPanel}"
                                        CommandParameter="{Binding}"/>   
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </phone:LongListSelector>

ViewModel

private ObservableCollection<Album> _albums;
    public ObservableCollection<Album> Albums
    {
        get { return _albums; }
        set
        {
            if (value != null)
            {
                _albums = value;
                NotifyPropertyChanged();
            }
        }
    }

    private Album _selectedAlbum;
    public Album SelectedAlbum
    {
        get { return _selectedAlbum; }
       // code removed as it is not needed; the object is null when trying to set.
}

        public void AlbumSelected(object p)
    {

        App.Dispatcher.BeginInvoke(() =>
            {
                SelectedAlbum = (Album)p;
            });

        ////Navigate("/Views/PhotosListPage.xaml");
    }

//command that takes an object as parameter.
            _selectAlbumCommand = new DelegateCommand<object>(this.AlbumSelected);
1

2 Answers 2

2

In case you merely want to set the SelectedAlbum by your SelectAlbumCommand, why don't you try binding the SelectedItem to SelectedAlbum instead?

<phone:LongListSelector x:Name="AlbumList" ItemsSource="{Binding Albums}" 
 SelectedItem="{Binding SelectedAlbum}" />

In case you actually want to pass the SelectedItem to the SelectedAlbumCommand (for some other reason), you should bind the CommandParameter to the SelectedItem of the LongListSelector

 <i:InvokeCommandAction Command="{Binding DataContext.SelectAlbumCommand, ElementName=ContentPanel}" CommandParameter="{Binding ElementName=AlbumList, Path=SelectedItem}"/>   
Sign up to request clarification or add additional context in comments.

3 Comments

+1, but in your first example, the SelectedItem binding would have to have Mode=TwoWay (?)
Yep.. TwoWay.. I think the default for LongListSelector's SelectedItem property is TwoWay.. No harm in specifying that again though
Thanks, but the object p is still coming up as null with your updated code for the CommandParameter and when I bind SelectedAlbum directly to SelectedItem, nothing happens when I click an item.
0

Apparently, you cannot use LongListSelector for this. I had to change this to a listbox and it worked fine.

Had I searched harder, I would have found this: How to select an item in LongListSelector using the MVVM-pattern?

and this: WP8 LongListSelector SelectedItem not bindable

1 Comment

Good find.. was about to suggest you the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.