简体   繁体   中英

INotifyPropertyChanged won't update the view

I've done this so many times, but this has got me stumped.

    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()));
            }
        }
    }

Simple notify property changed.

Very very simple custom class object

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

ViewModel

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
}

View

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

So simply put, you right click one of the test objects i put into the List<T> and then click Delete. this work and triggers the delegated command, the command then processes okay and the NotifyPropertyChanged(T) Triggers. there are no errors what so ever, however my view still shows the same three objects. but in the view model the List<T> shows only 2 objects.

Where have I gone wrong?

I think you should try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM