0
votes

How can I get the selecteditem from my listview in mvvm light wpf?

I have a collection and created a selected item property but I cant get the binding right for the selected item.

This is my viewmodel:

        ObservableCollection<DTO.Dossier.Dossier> _dossiers;
    public ObservableCollection<DTO.Dossier.Dossier> Dossiers
    {
        get { return _dossiers; }
        set
        {
            _dossiers = value;
            RaisePropertyChanged("Dossiers");
        }
    }

        private DTO.Dossier.Dossier _selectedDossier;
    public DTO.Dossier.Dossier SelectedDossier
    {
        get { return _selectedDossier; }
        set
        {
            if (_selectedDossier != value)
                _selectedDossier = value;

            RaisePropertyChanged("SelectedDossier");

        }
    }

And this is the xaml for the listview:

                <ListView  ItemsSource="{Binding Dossiers}" Margin="0,5,0,0" Name="LstDossiers" SelectedItem="{Binding SelectedDossier, Mode=OneWay}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <cmd:EventToCommand Command="{Binding SelectDossierCommand}"
                                CommandParameter="{Binding SelectedDossier,
                                      ElementName=LstDossiers}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Id"  
        DisplayMemberBinding="{Binding Id}" />
                        <GridViewColumn Header="Omschrijving" 
        DisplayMemberBinding="{Binding Omschrijving}" />
                    </GridView>
                </ListView.View>
            </ListView>

The command is working but the binding to the SelectedDossier isn't.

1
Change the BindingMode to TwoWay!!!Jawahar
Off course..thanks Mr Lovalova :)Kaizer

1 Answers

4
votes

You have to use mode TwoWay in your binding:

<ListView  ItemsSource="{Binding Dossiers}" Margin="0,5,0,0" Name="LstDossiers" SelectedItem="{Binding SelectedDossier, Mode=TwoWay}">