【问题标题】:Why ListView doesn't update in MVVM [duplicate]为什么 ListView 在 MVVM 中不更新 [重复]
【发布时间】:2017-02-24 09:35:52
【问题描述】:

我有一个小问题,我不明白它来自哪里,我想当我得到答案时,我可能会说著名的“aaaaahhhhh yessss!当然!”所以这是我的问题:我尝试通过拖放更新 mvvm 中的listView,使用断点和东西我可以看到进入listViewList<string> 已更新并且里面有一个新项目,我传递给listView 项目的元素,但视图没有更新,新项目也没有出现。这是我的代码:

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

当我调试时,我明白了:

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

所以我们可以看到添加了algo.pdf,但是视图没有更新。我错过了什么?!

非常感谢!

【问题讨论】:

  • 同时显示列表视图 xaml
  • 这里是@Ehsan Sajjad
  • @EhsanSajjad:它们实际上不是同一个问题 - 并且该问题没有被接受/正确的答案。这个人的问题是他没有使用ObservableCollection,您链接的问题已经使用了一个并且仍然存在问题。你能重新打开吗?

标签: c# wpf listview mvvm view


【解决方案1】:

List&lt;&gt; 无法通知 WPF 已添加项目,因此 WPF 无法更新 UI 以显示任何已添加的项目。

尝试使用ObservableCollection&lt;&gt;,它会在您添加/删除项目时向 WPF 发送通知事件。

另外,请记住,如果您希望 WPF 在这些对象中的属性发生更改时更新这些对象,则集合中的项目也应该实现 INotifyPropertyChanged

【讨论】:

  • 是的,我今天刚学到新东西!谢谢@Caesay
猜你喜欢
  • 2011-06-24
  • 2017-04-22
  • 1970-01-01
  • 2016-12-17
  • 2014-09-24
  • 2021-11-15
  • 1970-01-01
  • 2017-01-07
  • 2020-08-09
相关资源
最近更新 更多