我需要列出项目.我将用户集合绑定到列表框.一切都很好,但列表框中的项目不会实时更新.它们根本没有更新.因此,当我从列表中删除任何用户时,即使其源已正确更改,列表框也不会更新.
Source位于路径DataViewModel.Instance.AllUsers的数据视图模型中; 每当我向此列表添加新项目或删除一项时,布局都不会更新.其他绑定工作得很好.我试图更新列表框布局,提出源更新事件,添加/删除项目的其他方式,但没有任何工作.
我试图调试绑定,但我有太多的绑定来找到错误.
提前感谢任何有用的建议.
列表框:
<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"/>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
CatalogueGrid.DataContext = DataViewModel.Instance; //whole view model added as datacontext
Run Code Online (Sandbox Code Playgroud)
DataViewModel类:
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));
}
}
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
用ObservableColLection,而是如果Collection至极实现的INotifyCollectionChanged接口:
private ObservableCollection<UserModel> allUsers;
public ObservableCollection<UserModel> AllUsers
{
get
{
return allUsers;
}
set
{
allUsers = value;
NotifyPropertyChanged("AllUsers");
}
}
Run Code Online (Sandbox Code Playgroud)