WPF MVVM ListBox所选项目仅在第一次更改

时间:2018-12-17 20:17:49

标签: c# wpf xaml mvvm

我想补充一点,在发布之前,我已经在Google上搜索过,因此已经找到了答案,但是似乎没有一种解决方案适合我。

我试图从WPF列表框中选择一个“人物”,并将我的视图模型上的属性更新为所选的“人物”。第一次选择项目时效果很好。第一次选择后,更改为另一个项目不会更新视图模型“ Person”。

ViewModel:

public class PersonViewModel : BaseViewModel
{
    private Person _selectedPerson;
    private ICommand _addToPeopleCommand;
    private ICommand _removeFromPeopleCommand;
    public Person NewPerson { get; set; }
    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set
        {
            _selectedPerson = value;
            OnPropertyChanged(nameof(SelectedPerson));
        }
    }
    public ObservableCollection<Person> PeopleList { get; set; }
    public ICommand AddToPeopleList
    {
        get
        {
            if (_addToPeopleCommand == null)
            {
                _addToPeopleCommand = new RelayCommand(
                    p => true,
                    p => this.PeopleList.Add(this.NewPerson));
            }
            return _addToPeopleCommand;
        }
    }

    public ICommand RemoveFromPeopleList
    {
        get
        {
            if (_removeFromPeopleCommand == null)
            {
                _removeFromPeopleCommand = new RelayCommand(
                    p => true,
                    p => this.PeopleList.Remove(SelectedPerson));
            }
            return _removeFromPeopleCommand;
        }
    }


    public PersonViewModel()
    {
        this.NewPerson = new Person();
        this.PeopleList = new ObservableCollection<Person>();
    }
}

BaseViewModel实现INotifyPropertyChanged。万一有人想知道。

还有XAML:

<Window x:Class="MVVM_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:viewModels="clr-namespace:MVVM_Test"
    d:DataContext="{d:DesignInstance viewModels:PersonViewModel}"
    mc:Ignorable="d"
    Title="MainWindow" Height="397" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding NewPerson.FirstName}" 
             Width="200"
             Grid.Row="0"/>
    <TextBox Text="{Binding NewPerson.LastName}" 
             Width="200"
             Grid.Row="1"/>
    <TextBox Text="{Binding NewPerson.FullName}" 
             Width="200"
             Grid.Row="2"
             IsReadOnly="True"/>
    <Button Grid.Row="3" 
            Content="Add"
            Margin="50,10,151,10"
            Command="{Binding AddToPeopleList}"/>
    <ListBox x:Name="listBox" 
             Margin="10,11,-206,-257"
             ItemsSource="{Binding PeopleList}"
             SelectedItem="{Binding SelectedPerson, UpdateSourceTrigger=PropertyChanged}"
             Grid.Row="4">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button x:Name="remove" Content="Remove" 
            HorizontalAlignment="Left" 
            Margin="155,10,0,0" 
            Grid.Row="3" 
            VerticalAlignment="Top" 
            Width="75" 
            RenderTransformOrigin="0.067,0.9"
            Command="{Binding RemoveFromPeopleList}"
            CommandParameter="{Binding ElementName=listBox, Path=SelectedPerson}"/>
    <TextBlock x:Name="textBlock" 
               HorizontalAlignment="Left" 
               Margin="311,15,-206,0" 
               TextWrapping="Wrap" 
               Text="{Binding SelectedPerson.FullName}" 
               VerticalAlignment="Top" 
               Width="196" 
               Height="75" 
               Grid.RowSpan="4"/>
</Grid>

最终目标是从列表框中选择一个人,并将其从ObservableCollection中移除。我可以将人员从包装箱中取出,直到只剩下一个人为止,这时它停止工作。但是,如上所述,当我选择其他人时,将其从列表中删除后,将不再选择要删除的任何人。选择第二个人也不会第二次致电设置员。我什至通过包含TextBlock并将其绑定到选定的person属性进行了测试。第一次只更新一次。

1 个答案:

答案 0 :(得分:1)

您的AddToPeopleList命令一次又一次添加相同的Person实例。 更改将NewPerson副本添加到列表的方法可能会解决您的问题。