简体   繁体   English

WPF Datagrid:更新项目源的简单方法

[英]WPF Datagrid: Update itemssource the simple way

first of all, I normally write code using MVVM or MVVMC, but for a very simple project, I want to try doing everything the "old way", meaning writing a simple to understand app using only code behind logic, and no INotifyPropertyChanged interface. 首先,我通常使用MVVM或MVVMC编写代码,但是对于一个非常简单的项目,我想尝试以“旧方式”进行所有操作,这意味着仅使用逻辑代码而不使用INotifyPropertyChanged接口编写一个易于理解的应用程序。

For that purpose, I have created a very simple Employee sample app with a class that loads a list of Employees to an Obersvablecolletion. 为此,我创建了一个非常简单的Employee示例应用程序,该应用程序带有一个类,用于将Obersvablecolletion中的Employees列表加载。 The problem ist: after I set the Itemssource and DataContext, after Loading, my DataGrid does not get updated. 问题是:设置Itemssource和DataContext后,在加载后,我的DataGrid无法更新。 Of course I could set the DataContext again after loading, but is there a better way to do so? 当然,加载后我可以再次设置DataContext,但是有更好的方法吗? Some kind of telling the DataGrid in code behind that its contents have changed and Invaldiate them? 在某种程度上告诉代码背后的DataGrid其内容已更改并对其进行Invaldiate?

Here is my sample code: 这是我的示例代码:

public partial class MainWindow : Window
{
    private EmployeeList _MyList;

    public MainWindow()
    {
        InitializeComponent();
         _MyList= new EmployeeList();            
         _MyList.Employees = new ObservableCollection<Employee>();                        
        _MyGrid.DataContext = _MyList;
        _MyGrid.ItemsSource = _MyList.Employees;
    }

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void _AddButton_Click(object sender, RoutedEventArgs e)
    {
        AddWindow newWindow = new AddWindow();
        if (newWindow.ShowDialog() == true)
        {
            _MyList.Employees.Add(newWindow.NewEmployee);
        }

    }

    private void _LoadButton_Click(object sender, RoutedEventArgs e)
    {
        _MyList.Load();            
        //Creates new_MyList.Employees and fills with content from a file. After that, my DataGrid does not get updated
    }

Do not create a new _MyList.Employees collection instance. 不要创建新的_MyList.Employees集合实例。

Instead clear and re-fill the existing one: 而是清除并重新填充现有的:

_MyList.Employees.Clear();

for (var employee in employeesFromFile)
{
    _MyList.Employees.Add(employee);
}

Since you aren't using a Binding for _MyGrid.ItemsSource , it's also not necessary to set _MyGrid.DataContext . 由于您没有为_MyGrid.ItemsSource使用绑定,因此也不必设置_MyGrid.DataContext

If you are using ObservableCollection , then DataGrid will update itself without any extra issues. 如果您使用的是ObservableCollection ,那么DataGrid会自动更新,而不会出现任何其他问题。

But if you are using List<Employee> , then you need to re-assign the ItemsSource like below : 但是,如果您使用List<Employee> ,则需要像下面这样重新分配ItemsSource

private void _AddButton_Click(object sender, RoutedEventArgs e)
    {
        AddWindow newWindow = new AddWindow();
        if (newWindow.ShowDialog() == true)
        {
            _MyList.Employees.Add(newWindow.NewEmployee);

            // re-assign the `ItemsSource`
            _MyGrid.ItemsSource = null;
            _MyGrid.ItemsSource = _MyList.Employees;
        }
    }

You can remove the existing item-source by assigning null value to the data-grid and then assign updated item-source like, 您可以通过为数据网格分配空值来删除现有的item-source,然后分配更新的item-source,例如,

_MyGrid.ItemsSource = null;
_MyGrid.ItemsSource = _MyList.Employees;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM