【发布时间】:2013-12-19 21:28:37
【问题描述】:
运行以下代码不会从列表框中删除项目(在用户看来)
lbxUserSecurityGroups.ItemsSource = _currentUserGroups;
_currentUserGroups.RemoveAt(0);
lbxUserSecurityGroups.ItemsSource = _currentUserGroups;
但这会:
lbxUserSecurityGroups.ItemsSource = _currentUserGroups;
_currentUserGroups.RemoveAt(0);
lbxUserSecurityGroups.ItemsSource = null;
lbxUserSecurityGroups.ItemsSource = _currentUserGroups;
我的猜测是,由于我对 ItemsSource 使用相同的对象,因此列表框不会更新,因为它认为没有理由更新(例如,仅在项目源发生更改时才更新它)
有没有办法强制 ItemsSource 更新,例如:
lbxUserSecurityGroups.UpdateItemsSource();
注意:我知道执行此操作的正确方法是使用 ObservableCollection。但这让我觉得奇怪的行为,我希望能够通过在将值设置为正确值之前将值设置为 null 来完成我正在尝试的事情。
【问题讨论】:
-
WPF / silverlight DepedencyProperty 机制确保在更新 UI / 执行任何操作之前值已更改。您将
same exact reference设置为 ItemsSource,无论该对象的内容是否已更改。
标签: c# silverlight itemssource