MVVM light listview selecteditem

时间:2015-01-12 06:58:13

标签: wpf listview mvvm-light

如何从我的listview中获取mvvm light wpf中的selecteditem?

我有一个集合并创建了一个选定的项目属性,但我无法获得所选项目的绑定权。

这是我的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");

        }
    }

这是listview的xaml:

                <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>

该命令正在运行,但与SelectedDossier的绑定不是。

1 个答案:

答案 0 :(得分:4)

您必须在绑定中使用模式TwoWay:

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