INotifyPropertyChanged 不会更新视图

INotifyPropertyChanged won't update the view

我已经做过很多次了,但这让我很困惑。

    public class ObservableObject : System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(Object PropertyValue)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(PropertyValue.ToString()));
            }
        }
    }

简单通知 属性 已更改。

非常非常简单的自定义class对象

class Device : ObservableObject
{
    public String DeviceName
    {
        get { return _DeviceName; }
        set { _DeviceName = value; NotifyPropertyChanged("DeviceName"); }
    }
    private String _DeviceName = "";

    public Device() { }
    public Device(String ComputerName)
    {
        DeviceName = ComputerName;
    }
}

视图模型

class MainWindowViewModel :ObservableObject
{
    public Device SelectedDevice { get; set; }
    public List<Device> DeviceList
    {
        get { return _DeviceList; }
        set { _DeviceList = value; NotifyPropertyChanged("DeviceList"); }
    }
    public List<Device> _DeviceList = new List<Device>();
    public MainWindowViewModel()
    {
        CreateDelegateCommands();
        DeviceList.Add(new Device("Metabox-PC"));
        DeviceList.Add(new Device("NewPC"));
        DeviceList.Add(new Device("OldPC"));
    }
    #region Delegated Commands
    public Boolean CreateDelegateCommands()
    {
        try
        {
            ContextMenuCommand = new DelegateCommand(DelegatedContextMenuCommand);
            return true;
        }
        catch
        {
            return false;
        }
    }
    public DelegateCommand ContextMenuCommand { get; set; }

    public void DelegatedContextMenuCommand(Object Parameter)
    {
        System.Windows.Controls.MenuItem MenuItemClicked = Parameter as System.Windows.Controls.MenuItem;
        switch (MenuItemClicked.Header.ToString())
        {
            case "Delete": { DeviceList.Remove(SelectedDevice); NotifyPropertyChanged("DeviceList"); break; }
        }
    }
    #endregion
}

查看

 <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="102*"/>
            <ColumnDefinition Width="415*"/>
        </Grid.ColumnDefinitions>
        <ListView ItemsSource="{Binding DeviceList}" SelectedItem="{Binding SelectedDevice,Mode=TwoWay}">
            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete" Command="{Binding ContextMenuCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                </ContextMenu>
            </ListView.ContextMenu>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Device" DisplayMemberBinding="{Binding DeviceName}"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>

所以简单地说,您右键单击我放入 List<T> 的测试对象之一,然后单击删除。这项工作并触发委托命令, 然后命令处理正常,NotifyPropertyChanged(T) 触发器。没有任何错误,但是我的视图仍然显示相同的三个对象。但在视图模型中 List<T> 仅显示 2 个对象。

我哪里做错了?

我觉得你应该试试

  public ObservableCollection<Device> DeviceList
    {
        get { return _DeviceList; }
        set { _DeviceList = value; NotifyPropertyChanged("DeviceList"); }
    }
    public ObservableCollection<Device> _DeviceList = new ObservableCollection<Device>();