-1

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?

Maarten
  • 22,527
  • 3
  • 47
  • 68
New Bee
  • 991
  • 1
  • 13
  • 24

1 Answers1

2

I think you should try

  public ObservableCollection<Device> DeviceList
    {
        get { return _DeviceList; }
        set { _DeviceList = value; NotifyPropertyChanged("DeviceList"); }
    }
    public ObservableCollection<Device> _DeviceList = new ObservableCollection<Device>();
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • This does work, is there a limitation using Ltst with INotifyPropertyChanged ? – New Bee Mar 12 '15 at 22:52
  • ObservableCollection implements `INotifyCollectionChanged` which provides notification when the collection is changed whereas you are implementing `INotifyPropertyChanged`..got the point – Rohit Mar 13 '15 at 05:09