【发布时间】:2017-02-24 09:35:52
【问题描述】:
我有一个小问题,我不明白它来自哪里,我想当我得到答案时,我可能会说著名的“aaaaahhhhh yessss!当然!”所以这是我的问题:我尝试通过拖放更新 mvvm 中的listView,使用断点和东西我可以看到进入listView 的List<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,您链接的问题已经使用了一个并且仍然存在问题。你能重新打开吗?