0

I have a little problem and I don't understand where it comes from, I suppose when I will get the answer I will probably said the famous " aaaaaahhhhh yessss ! of course !" so here his my problem : I try to update a listView in mvvm with a drag and drop, with the breakpoints and stuff I can see that the List<string> that goes into the listView is updated and has a new item inside, the element that I pass to the listView Items, but the view doesn't update and the new Item doesn't appear. Here is my code :

private List<string> _listViewItems;
public List<string> listViewItems
{
    get
    {
        return _listViewItems;
    }
    set
    {
        _listViewItems = value;
        OnPropertyChanged("listViewItems");
    }
}

public ICommand MouseMove { get; set; }

//in constructor
MouseMove = new BaseCommand(GoMouseMove);

private void GoMouseMove(object obj)
{
    MouseEventArgs e = (MouseEventArgs)obj;
    try
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            draggedItem = (TreeViewItem) SelectedItem;
            if (draggedItem != null)
            {
                DragDropEffects finalDropEffect = DragDrop.DoDragDrop(SelectedItem, SelectedItem, DragDropEffects.Move);

                //Checking target is not null and item is dragging(moving)
                if ((finalDropEffect == DragDropEffects.Move))
                {
                    CopyItem(draggedItem, _target);
                    _target = null;
                    draggedItem = null;
                }
            }                
        }
    }
    catch (Exception)
    {
    }
}

private void CopyItem(TreeViewItem _sourceItem, ListViewItem _targetItem)
{
    //Asking user wether he want to drop the dragged TreeViewItem here or not
    if (MessageBox.Show("Would you like to drop " + _sourceItem.Header.ToString(), "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
    {
        try
        {
            List<string> items = listViewItems;
            items.Add(_sourceItem.Header.ToString());
            listViewItems = items;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
}

When i debug i got that :

enter image description here

<ListView Name="listview1"
          Grid.ColumnSpan="2"
          Width="auto"
          Height="auto"
          Grid.Row="1"
          AllowDrop="True"
          ItemsSource="{Binding listViewItems, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Drop" >
            <cmd:EventToCommand Command="{Binding Drop}"
     PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

So we can see that algo.pdf is added, but the view doesn't update. What am I missing ?!

Thank you very much !

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Json
  • 153
  • 2
  • 7
  • 17
  • show the listview xaml as well – Ehsan Sajjad Feb 24 '17 at 09:38
  • here it's @Ehsan Sajjad – Json Feb 24 '17 at 09:40
  • @EhsanSajjad: They're actually not the same question - and there was no accepted / correct answer on that question. This guy's problem is that he wasn't using an `ObservableCollection`, the question you linked was already using one and still having issues. Can you re-open? – caesay Feb 24 '17 at 09:54

1 Answers1

3

A List<> has no way to inform WPF that an item has been added, and therefore WPF can not update the UI to display any added items.

Try using an ObservableCollection<> which will send notificiation events to WPF when you add / remove items.

Also, remember that the items inside your collection should also implement INotifyPropertyChanged if you want WPF to update when properties inside those objects change.

caesay
  • 16,932
  • 15
  • 95
  • 160