Wranglerino
Wranglerino

Reputation: 197

Bind SelectedItem to property on VM

In my Xaml I have this listview:

<ListView HorizontalAlignment="Left" 
                      Height="355" 
                      Margin="144,373,0,0" 
                      VerticalAlignment="Top" 
                      Width="154"
                      ItemsSource="{Binding People}"
                      SelectedItem="{Binding SelectedItem}"
                      ItemTemplate="{StaticResource Standard310x260ItemTemplate}"/>

And on my VM i have this property:

private Person _selectedItem;
        public Person SelectedItem
        {
            get
            {
                return _selectedItem;
            }

            set
            {
                if (_selectedItem == value || value == null)
                {
                    return;
                }

                var oldValue = _selectedItem;
                _selectedItem = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("SelectedItem");
            }
        }

What im hoping for is that the SelectedItem-property on my VM would get updated whenever I click an item in the listview. Can someone see what I am missing here?

Upvotes: 0

Views: 43

Answers (2)

luxas
luxas

Reputation: 438

Try with

<ListView SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

If it doesn't work, in page constructor add:

this.DataContext = this;

It should work now.

Hope it helps :)

Upvotes: 1

ShayD
ShayD

Reputation: 930

Try adding IsSynchronizedWithCurrentItem: MSDN

Upvotes: 0

Related Questions