Hello,
My simplfied model is a folder that contains files. I want to display a single folder in a propertyGrid and in that PropertyGrid I don't want a collection of files but a new line for each files.
The problem is that when there is new file I need to refresh manualy but if I remove my DataFactory it works, the propertyGrid refresh itself.
I made a simple model with the same issue to help you to understand :
Model :
public class Folder
{
public Folder(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Files = new ObservableCollection<File>();
}
public string Name { get; private set; }
public ObservableCollection<File> Files{ get; private set; }
}
public class File
{
public File(string name, string content)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Content = content ?? throw new ArgumentNullException(nameof(content));
}
public string Name { get; private set; }
public string Content { get; set; }
}
PropertyModel :
internal class FilePropertyModel : CachedPropertyModelBase
{
private File file;
public FilePropertyModel(File file)
{
this.file = file ?? throw new ArgumentNullException(nameof(file));
}
protected override bool CanResetValueCore => true;
protected override bool IsMergeableCore => true;
protected override bool IsModifiedCore => true;
protected override bool IsValueReadOnlyCore => true;
protected override string NameCore => file.Name;
protected override object TargetCore => file;
protected override object ValueCore { get => file.Content; set => file.Content = value.ToString(); }
protected override Type ValueTypeCore => typeof(string);
}
And DataFactory :
public class FolderDataFactory : TypeDescriptorFactory
{
public FolderDataFactory()
{
}
protected override IList<IPropertyModel> GetPropertyModels(object dataObject, IDataFactoryRequest request)
{
if(dataObject is Folder folder)
{
var propertyModels = new List<IPropertyModel>();
var propertyDescriptors = TypeDescriptor.GetProperties(folder);
propertyModels.Add(new PropertyDescriptorPropertyModel(folder, propertyDescriptors[nameof(folder.Name)]));
foreach (var file in folder.Files)
{
propertyModels.Add(new FilePropertyModel(file));
}
return propertyModels;
}
return base.GetPropertyModels(dataObject, request);
}
}
Thanks.