简体   繁体   English

我的dataGrid无法清除并且无法刷新

[英]My dataGrid doesn't clear and doesn't refresh

I have two DataGrid, each binding in a dataSource like this : 我有两个DataGrid,每个绑定在这样的dataSource中:

ItemsSource="{Binding Data, ElementName=EmpSource, Mode=TwoWay}"

The first DataGrid(dgJob), contains Job and the second(dgEmp), the employee linked to the job . 第一个DataGrid(dgJob)包含Job ,第二个DataGrid(dgEmp)链接到jobemployee

I want to keep all the employees in the EmpSource, and display in the dataGrid, only those who are linked to the selected job in my first datagrid. 我想将所有员工保留在EmpSource中,并在dataGrid中显示,只有那些链接到我的第一个datagrid中的所选作业的员工。

So I am doing this in the dgJob selectionChanged event : 所以我在dgJob selectionChanged事件中这样做:

dgEmp.ItemsSource = null;
var lstEmp = EmpSource.DataView.OfType<Emp>().Where(ores => ores.IdJob == itmJobSelect.IdJob).ToList();
dgEmp.ItemsSource = lstEmp;

The problem is, the dataGrid is not clearing when I change the selected line in my datagrid with the jobs, so for every job, I display every Employees in the dgEmp, while I should only display those who are connected to the job. 问题是,当我用作业更改数据网格中的选定行时,dataGrid不会清除,因此对于每个作业,我都会在dgEmp中显示每个Employees,而我应该只显示那些已连接到该作业的雇员。

I can delete the line in the xaml, that determine the dataSource, but if I do this, I must refresh the dataGrid when there is a change in the dataSource. 我可以删除xaml中确定dataSource的行,但是如果这样做,则必须在dataSource发生更改时刷新dataGrid。

But I don't found how to refresh it(at least for the first time) unless I write the 3 lines each time after a change in dataSource. 但是我没有找到如何刷新它(至少是第一次),除非我在dataSource更改后每次都写3行。

Can somebody help me find a solution to my problem? 有人可以帮我找到解决问题的办法吗?

Thank you. 谢谢。

I recommend you to use MVVM design pattern. 我建议您使用MVVM设计模式。 You should load your data in view model class and store it in collection which implements INotifyCollectionChanged interface. 您应该将数据加载到视图模型类中,并将其存储在实现INotifyCollectionChanged接口的集合中。 View model should also implement INotifyPropertyChanged interface. 视图模型还应该实现INotifyPropertyChanged接口。

When your employee collection changes, you should filter second collection as in following code: 当您的员工集合发生变化时,您应该按照以下代码过滤第二个集合:

Jobs.CollectionChanged += (sender, args) =>
{
    Employees = AllEmployees.Where(c=> c.IdJob == SelectedJob.IdJob);
}

You should also do same thing when SelectedJob changes and DataGrid will be refreshed. 当SelectedJob更改并且DataGrid将被刷新时,您还应该做同样的事情。

This will work only when you will have implemented property changed notifications and correct binding was specified. 仅当您已实现属性更改通知并指定了正确的绑定时,此方法才起作用。

Here's example of property changed implementation which you should write: 这是您应该编写的属性更改的实现示例:

public class ViewModel : INotifyPropertyChanged
{
    public IEnumerable<Emp> Employees
    {
       get { return _employees; }
       set
       {
           if (_employees != value)
           {
               _employees = value;
               OnPropertyChanged("Employees");
           }
       }
    }

    /* ... */

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

You should also assign your view model instance to DataContext for make binding works. 您还应该将视图模型实例分配给DataContext以便进行绑定。 For example in code behind file constructor: 例如,在文件构造函数后面的代码中:

public void Page()
{
    DataContext = new ViewModel();
    InitializeComponent();
}

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

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