在我的带有 MVVM 模式的 Silverlight5 项目中,我有一个列表框。我使用实体框架将数据加载到列表框。最初,所有数据都使用实体框架模型加载到列表框中。但是如果我添加新数据,列表框不会显示新添加的值。但是新增加的价值在实体中是可用的。问题是什么?我必须做些什么来实现这一点。
列表框 xaml 编码:
<ListBox ItemsSource="{Binding Projects,Mode=TwoWay}" SelectedItem="{Binding Project,Mode=TwoWay}" SelectionMode="Single" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!--<StackPanel Orientation="Horizontal" />-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" Margin="2,2,2,2" BorderThickness="0">
<StackPanel Orientation="Vertical" >
<TextBlock Text="{Binding ProjectName,Mode=TwoWay}"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
视图模型代码:
private IEnumerable<Project> projects;
private Project project;
private Customer customer;
PartsDomainContext _context;
public IEnumerable<Project> Projects
{
get
{
return projects;
}
set
{
projects= value;
if (projects != null)
{
OnPropertyChanged("Projects");
Project = projects.FirstOrDefault();
OnPropertyChanged("Project");
}
}
}
public Project Project
{
get
{
return project;
}
set
{
project=value;
OnPropertyChanged("Project");
if (project != null)
{
Customer = project.CustomerProjects.FirstOrDefault().Customer;
OnPropertyChanged("Customer");
}
}
}
public ProjectListViewModel()
{
GetProjectList(ActiveData.Instance.userid);
}
public void GetProjectList(int userid)
{
_context = new PartsDomainContext();
OnPropertyChanged("Projects");
_context.Load(_context.GetProjectListQuery(ActiveData.Instance.userid), Param =>
{
if (!Param.HasError)
{
Projects = Param.Entities;
OnPropertyChanged("Projects");
}
}, null);
}
public void GetProjectList(int userid)
{
_context = new PartsDomainContext();
OnPropertyChanged("Projects");
_context.Load(_context.GetProjectListQuery(ActiveData.Instance.userid), Param =>
{
if (!Param.HasError)
{
Projects = Param.Entities;
OnPropertyChanged("Projects");
}
}, null);
}
需要帮助解决它..!