调用 RaiseCanExecuteChanged 后 ListView 未更新
我有一个绑定到视图模型集合的 ListView。项目模板包含一个绑定到视图模型上的命令的按钮。当我设置 ListView 的 ItemsSource 的属性时,我为每个视图模型调用 RaiseCanExecuteChanged 。
public BindingList<IVehicleViewModel> Vehicles
{
get { return _vehicles; }
set
{
if(_vehicles == value) return;
_vehicles = value;
OnPropertyChanged("Vehicles");
RaiseCanExecuteChangedEvents();
}
}
尽管我已经验证所有视图模型上的 CanExecute 返回 true,但该按钮显示为灰色。 我注意到的唯一一件事是,如果我在列表视图已经呈现时调用 RaiseCanExecuteChanged ,那么一切都会按预期工作,并且如果我在列表视图呈现之前调用它,然后滚动浏览它们自行排序的项目。
I have a ListView that is bound to a collection of view models. The Item template contains a button that is bound to a command on the view model. When I set the property that the ItemsSource of the ListView I call RaiseCanExecuteChanged for each viewmodel.
public BindingList<IVehicleViewModel> Vehicles
{
get { return _vehicles; }
set
{
if(_vehicles == value) return;
_vehicles = value;
OnPropertyChanged("Vehicles");
RaiseCanExecuteChangedEvents();
}
}
Despite the fact that I have verified that true is returned for the CanExecute on all view models the button shows as greyed out.
The only thing I have noticed is that if I call RaiseCanExecuteChanged when the listview has already been rendered everything works as expected and if I call it before the listview has been rendered and then scroll through the items they sort themselves out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个静态方法 CommandManager.InvalidateRequerySuggested 会强制重新评估命令的可执行性(这是一个词吗?),每当您想要确保 UI 更新以反映 CanExecute 中的更改时,请尝试调用该方法 你的命令的结果。
There's a static method
CommandManager.InvalidateRequerySuggested
which forces a reevaluation of command executability (is that a word?) try calling that whenever you want to make sure your UI updates to reflect a change in theCanExecute
result of your command.