我的PropertyChanged适用于我的页面上没有绑定到Datagrids的所有对象,这些对象在初始绑定时正常工作,但在修改或重置绑定属性时会失去绑定。
请注意,我已尝试使用ObservableCollection并获得相同的结果。
<DataGrid AutoGenerateColumns="False" Height="263" HorizontalAlignment="Left" Margin="50,120,0,0" ItemsSource="{Binding Files}" Name="dg_Display" VerticalAlignment="Top" Width="313">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FileName}" Header="File Name" />
<DataGridTextColumn Binding="{Binding Date}" Header="Date" />
<DataGridCheckBoxColumn Binding="{Binding Stack}" Header="Stack" />
</DataGrid.Columns>
</DataGrid>
private List<StackModel.Display> m_files;
public List<StackModel.Display> Files
{
get { return m_files ?? (m_files = StackModel.ListFiles(Path)); }
set
{
m_files = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Files"));
}
}
}
private DelegateCommand m_directory;
public ICommand DirectoryCommand { get { return m_directory ?? (m_directory = new DelegateCommand(Directory)); } }
private void Directory()
{
Files = StackModel.ListFiles(Path);
}
public List<Display> ListFiles(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles("*.xlsx");
return files.Select(fileEntry => new Display {Date = fileEntry.LastWriteTime, FileName = fileEntry.Name, Stack = true}).ToList();
}
答案 0 :(得分:2)
尝试代替
public List<StackModel.Display> Files
{
..
}
使用
public ObservableCollection<StackModel.Display> Files
{
..
}
根据我的理解,您的问题是在加载时正确加载集合中存在的所有数据,但是如果您清除该集合,删除或添加新项目,则更改不会对UI产生影响。
ObservableCollection应该为您解决此问题。
应该工作..