我在这里读了十几个主题,我无法弄清楚我做错了什么。
我有一个ObservableCollection,我可以添加新的Employees,但UI不会立即显示更改。如果我停止并重新启动,则会显示更改。
public class MainViewModel : ObservableObject
{
DataAccess.EmployeeRepository repository = new DataAccess.EmployeeRepository();
private ObservableCollection<Employee> employees;
public MainViewModel()
{
employees = new ObservableCollection<Employee>(repository.GetEmployees());
}
public ObservableCollection<Employee> Employees
{
get { return employees; }
set
{
employees = value;
OnPropertyChanged("Employees");
}
}
我的XAML具有以下绑定:
<ListView Name="ListViewEmployeeDetails" Grid.Row="1" Margin="4,109,12,23" ItemsSource="{Binding Employees}" SelectedItem="{Binding CurrentEmployee}" >
<ListView.View>
<GridView x:Name="grdTest">
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding EmployeeID}" Width="100"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding EmployeeName}" Width="100" />
<GridViewColumn Header="Phone" DisplayMemberBinding="{Binding EmployeePhone}" Width="100" />
</GridView>
</ListView.View>
</ListView>
我无法弄清楚出了什么问题。非常感谢任何帮助!
答案 0 :(得分:0)
public MainViewModel()
{
Employees = new ObservableCollection<Employee>(repository.GetEmployees());
}
您必须设置Employees
,因为您在其设置器上触发了NotifyPropertyChanged
事件。