我想實現一個數據網格,它將每5分鐘填充一次。如何刷新/重新加載WPF中的數據網格?
但是,第一碼: XAML:
<Window.Resources>
<ObjectDataProvider x:Key="_employeeProvider" ObjectType="{x:Type Brw1:EmployeeDataProvider}" />
<ObjectDataProvider x:Key="_employeeFactory" ObjectInstance="{StaticResource _employeeProvider}" MethodName="GetEmployees" />
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource _employeeFactory}}">
<DataGrid Name="_employeeDataGrid" DockPanel.Dock="Top" Margin="12,57,12,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="True">
</DataGrid>
</Grid>
C#:
public class EmployeeDataProvider
{
public ObservableEmployee GetEmployees()
{
// This is only example
// In my project right here I will get data from database, put into the list, and fill datagrid with it.
List<UIEmployee> list = new List<UIEmployee>
{
new UIEmployee { Id = 1, FirstName = "Prajeesh2", LastName = "Prathap", Occupation = "Engineer", DateOfJoining=new DateTime(2005, 8, 1), IsContracter = false, AddressInfo = null },
new UIEmployee { Id = 2, FirstName = "Rahul2", LastName = "Bose", Occupation = "Student", DateOfJoining=new DateTime(2009, 5, 4), IsContracter = true, AddressInfo = null },
new UIEmployee { Id = 3, FirstName = "Steve2", LastName = "Roberts", Occupation = "Manager", DateOfJoining=new DateTime(1994, 8, 23), IsContracter = false, AddressInfo = null },
new UIEmployee { Id = 4, FirstName = "Micheal2", LastName = "Clarke", Occupation = "Engineer", DateOfJoining=new DateTime(2003, 1, 14), IsContracter = true, AddressInfo = null },
new UIEmployee { Id = 5, FirstName = "Rachel2", LastName = "Green", Occupation = "Professional", DateOfJoining=new DateTime(2006, 3, 8), IsContracter = true, AddressInfo = null }
};
ObservableEmployee employeeCollection = new ObservableEmployee(list);
return employeeCollection;
}
}
這是非常簡單的。 Datagrid使用GetEmployees()
方法獲取數據。
我嘗試做的,就是運行GetEmployess每隔5min ..
PS。 _employeeDataGrid.Items.Refresh();
- 不起作用。
UPDATE:
缺少類:
public class ObservableEmployee : ObservableCollection<UIEmployee>
{
public ObservableEmployee(IEnumerable<UIEmployee> employees) : base(employees) { }
}
public class UIEmployee// : INotifyPropertyChanged
{
private int m_Id;
public int Id
{
get { return m_Id; }
set
{
m_Id = value;
}
}
private string m_FirstName;
public string FirstName
{
get { return m_FirstName; }
set
{
m_FirstName = value;
}
}
private string m_LastName;
public string LastName
{
get { return m_LastName; }
set
{
m_LastName = value;
}
}
private string m_Occupation;
public string Occupation
{
get { return m_Occupation; }
set
{
m_Occupation = value;
}
}
private DateTime m_DateOfJoining;
public DateTime DateOfJoining
{
get { return m_DateOfJoining; }
set
{
m_DateOfJoining = value;
}
}
private bool m_IsContracter;
public bool IsContracter
{
get { return m_IsContracter; }
set
{
m_IsContracter = value;
}
}
}
UPDATE2: 我需要的東西,這將重新加載數據網格,以使DataGrid將不得不再次使用GetEmployees
方法。
UPDATE3:
public class TaskDataProvider
{
static ObservableEmployee list = new ObservableEmployee();
static DataRepository dr = new DataRepository();
public static ObservableEmployee GetEmployees()
{
UpdateMyList();
return list;
}
public static void UpdateMyList()
{
ObservableTask newList = new ObservableTask(dr.GetTasks());
list.Clear();
foreach (Task t in newList)
{
list.Add(t);
}
}
}
這在開始工作。 但是,我改變數據庫某物,並單擊該做的按鈕:
TaskDataProvider.UpdateMyList();
而且在破發點,這是得到正確的數據(新數據) - 但是,數據網格是不爽快:/
你能請,張貼代碼,您實際上分配到的DataContext收集了來自GetEmployees()? – Tigran
我做到了。就這樣。只有缺失的代碼有兩種方法: UIEmployee和ObservableEmployee。 Datagrid使用父Grid的DataContext,所以「_employeeFactory」和這個類使用方法「GetEmployees」,它返回ObservableEmployee。沒有更多的代碼。 – Marshall