C# 我的数据网格没有';无法清除并不';t刷新
我有两个DataGrid,每个绑定在一个数据源中,如下所示:C# 我的数据网格没有';无法清除并不';t刷新,c#,silverlight,datagrid,datasource,C#,Silverlight,Datagrid,Datasource,我有两个DataGrid,每个绑定在一个数据源中,如下所示: ItemsSource="{Binding Data, ElementName=EmpSource, Mode=TwoWay}" 第一个数据网格(dgJob)包含作业,第二个数据网格(dgEmp)包含链接到作业的员工 我希望将所有员工保留在EmpSource中,并在dataGrid中显示,仅显示链接到我的第一个dataGrid中所选作业的员工 因此,我在dgJob selectionChanged事件中执行此操作: dgEmp.It
ItemsSource="{Binding Data, ElementName=EmpSource, Mode=TwoWay}"
第一个数据网格(dgJob)包含作业
,第二个数据网格(dgEmp)包含链接到作业
的员工
我希望将所有员工保留在EmpSource中,并在dataGrid中显示,仅显示链接到我的第一个dataGrid中所选作业的员工
因此,我在dgJob selectionChanged事件中执行此操作:
dgEmp.ItemsSource = null;
var lstEmp = EmpSource.DataView.OfType<Emp>().Where(ores => ores.IdJob == itmJobSelect.IdJob).ToList();
dgEmp.ItemsSource = lstEmp;
dgEmp.ItemsSource=null;
var lstEmp=EmpSource.DataView.OfType().Where(ores=>ores.IdJob==itmJobSelect.IdJob.ToList();
dgEmp.ItemsSource=lstEmp;
问题是,当我用作业更改dataGrid中的选定行时,dataGrid没有清除,因此对于每个作业,我都会在dgEmp中显示每个员工,而我应该只显示那些连接到作业的员工
我可以删除xaml中确定数据源的行,但如果我这样做,我必须在数据源发生更改时刷新数据网格
但我不知道如何刷新它(至少是第一次),除非每次在数据源发生更改后我都写这3行
有人能帮我找到解决问题的办法吗
谢谢。我建议您使用MVVM设计模式。您应该在视图模型类中加载数据,并将其存储在实现INotifyCollectionChanged接口的集合中。视图模型还应实现INotifyPropertyChanged接口 当您的员工集合发生更改时,应按以下代码筛选第二个集合:
Jobs.CollectionChanged += (sender, args) =>
{
Employees = AllEmployees.Where(c=> c.IdJob == SelectedJob.IdJob);
}
当SelectedJob更改并且DataGrid将被刷新时,您也应该做同样的事情
仅当您已实现属性更改通知并且指定了正确的绑定时,此操作才有效
下面是您应该编写的属性更改实现示例:
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));
}
}
}
您在DataGrid上尝试过.Refresh()吗?我的DataGrid上没有可用的.Refresh()Datagrid没有为Refresh()定义。Refresh()是为DataGridView定义的。不适用于DataGrid。先生,第一和第二个数据网格的名称是什么?
public void Page()
{
DataContext = new ViewModel();
InitializeComponent();
}