ListView.SelectedItem does not change when the property of the bound ConfigurationElement has changed

340 Views Asked by At

I have a class derived from ConfigurationElement as follows:

public class Item : ConfigurationElement, INotifyPropertyChanged
{
    // Binding Properties.
    [ConfigurationProperty("Name", DefaultValue = null, IsRequired = true)]
    public string Name
    {
        get
        {
            return this["Name"] as string;
        }
        set
        {
            this["Name"] = value;
            NotifyPropertyChanged();
        }
    }

    // Methods.
    private void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // ctor.
    public Item(string name)
    {
        Name = name;
    }

    // INotifyPropertyChanged members.
    public event PropertyChangedEventHandler PropertyChanged;
}

I have a View which contains a ListView, as follow:

<DockPanel>
    <Button DockPanel.Dock="Top" Content="Modify" Command="{Binding ModifyCommand}"/>
    <ListView DockPanel.Dock="Bottom" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">            
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</DockPanel>

The ViewModel for the View is as follow:

public class MainViewModel : Observant
{
    // Fields.
    public Item m_SelectedItem;

    // Binding Properties.
    public ObservableCollection<Item> Items
    {
        get;
        set;
    }
    public Item SelectedItem
    {
        get
        {
            return m_SelectedItem;
        }
        set
        {
            if (m_SelectedItem != value)
            {
                m_SelectedItem = value;
                // Updated.
                NotifyOfPropertyChange();
            }
        }
    }
    public RelayCommand ModifyCommand
    {
        get;
        set;
    }

    // ctor.
    public MainViewModel()
    {
        ModifyCommand = new RelayCommand(Modify_Execute);
        Items = new ObservableCollection<Item>();
        Items.Add(new Item("Bird"));
        Items.Add(new Item("Bug"));
        Items.Add(new Item("Cat"));
        Items.Add(new Item("Dog"));
        Items.Add(new Item("Fish"));
        Items.Add(new Item("Sheep"));
        Items.Add(new Item("Shrimp"));
    }

    private void Modify_Execute(object parameter)
    {
        m_SelectedItem.Name = string.Format("{0}-{1}", m_SelectedItem.Name, DateTime.Now.Millisecond);
    }
}

where Observant is a base class that implements INotifyPropertyChanged.

After selecting an item in the ListView, I've update the item in the ViewModel thru SelectedItem property (by clicking on the Modify button) and it went well. However, when I attempt to select another item after modifying current item, current item does not get deselected, as follow:

enter image description here

I'm aware that Item has different implementation of property getter and setter when compared to implementation that uses field. It is implemented as such because the Item is part of the configuration which is loaded from / persisted to a configuration file.

My question is: Is it possible correct this without using the getter/setter that uses field?

0

There are 0 best solutions below