【问题标题】:WPF MVVM ListBox Selected Item Only Changes First TimeWPF MVVM ListBox 选定项仅第一次更改
【发布时间】:2018-12-17 20:17:49
【问题描述】:

我想补充一下,我已经在 Google 上搜索过,所以在我发布之前,我已经找到了这个问题的答案,但似乎没有一个解决方案适合我。

我正在尝试从 WPF 列表框中选择一个“Person”,并将我的视图模型上的一个属性更新为所选的“Person”。我第一次选择一个项目时它工作正常。第一次选择后,更改为另一个项目不会更新视图模型“Person”。

视图模型:

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 属性来测试它。它只更新一次,第一次。

【问题讨论】:

  • 可能与您的问题无关,但 AddToPeopleList 命令一次又一次地添加相同的 Person 实例。
  • 您能否将此添加为答案,以便我将其标记为答案。这解决了我的问题。应该很明显,但我完全忽略了它。谢谢!

标签: c# wpf xaml mvvm


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多