我需要列出物品清单。我将用户集合绑定到列表框。一切都运行良好,但列表框中的项目不会实时更新。它们根本不会通过此绑定进行更新。因此,当我从列表中删除任何用户时,即使正确更改了列表框的源,列表框也不会更新。
源位于数据视图模型中的路径 DataViewModel.Instance.AllUsers;每当我向此列表添加新项目或删除一项时,布局都不会更新。其他绑定效果很好。我尝试更新列表框布局,引发源更新事件,添加/删除项目的其他方式,但没有任何效果。
我尝试调试绑定,但绑定太多,无法找到错误。
预先感谢您提供任何有用的建议。
Listbox:
<ListBox x:Name="ListboxUsers" ItemsSource="{Binding Path=AllUsers, Mode=OneWay}" Grid.Column="1" Margin="0" Grid.Row="5" Background="DimGray" BorderThickness="0" Visibility="Hidden" SelectionChanged="ListboxUsers_SelectionChanged"/>
隐藏代码:
CatalogueGrid.DataContext = DataViewModel.Instance; //whole view model added as datacontext
数据视图模型类:
public class DataViewModel : INotifyPropertyChanged
{
private static DataViewModel _dataViewModel;
private Collection<UserModel> allUsers;
public Collection<UserModel> AllUsers
{
get
{
return allUsers;
}
set
{
allUsers = value;
NotifyPropertyChanged("AllUsers");
}
}
private DataViewModel()
{
AllUsers = new Collection<UserModel>();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
.
.
.
}
use ObservableColLection
相反,如果Collection
它实现了INotifyCollectionChanged
界面 :
private ObservableCollection<UserModel> allUsers;
public ObservableCollection<UserModel> AllUsers
{
get
{
return allUsers;
}
set
{
allUsers = value;
NotifyPropertyChanged("AllUsers");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)