C# PropertyGrid在更改属性后不会更新
在我的WPF项目中,我使用的是PropertyGrid。我设置了SelectedObject并正确显示了数据 我还添加了以下内容,如果用户更改SelectedObject中某个属性的值,则某些属性将被隐藏,而其他属性将被显示C# PropertyGrid在更改属性后不会更新,c#,wpf,propertygrid,C#,Wpf,Propertygrid,在我的WPF项目中,我使用的是PropertyGrid。我设置了SelectedObject并正确显示了数据 我还添加了以下内容,如果用户更改SelectedObject中某个属性的值,则某些属性将被隐藏,而其他属性将被显示 PropertyDescriptorCollection pdcSld = TypeDescriptor.GetProperties(this.sld.GetType()); PropertyDescriptor descriptorF0 = pdcSld["f0Sl
PropertyDescriptorCollection pdcSld = TypeDescriptor.GetProperties(this.sld.GetType());
PropertyDescriptor descriptorF0 = pdcSld["f0Sld"];
PropertyDescriptor descriptortcStar = pdcSld["tcStarSSld"];
PropertyDescriptor descriptorGammaI = pdcSld["gammaI"];
PropertyDescriptor descriptorSpectrum = pdcSld["spectrum"];
if (descriptorF0 != null)
{
BrowsableAttribute attrib = (BrowsableAttribute)descriptorF0.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib, true);
}
if (descriptortcStar != null)
{
BrowsableAttribute attrib = (BrowsableAttribute)descriptortcStar.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow =
attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib, true);
}
if (descriptorGammaI != null)
{
BrowsableAttribute attrib = (BrowsableAttribute)descriptorGammaI.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow =
attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib, false);
}
if (descriptorSpectrum != null)
{
BrowsableAttribute attrib = (BrowsableAttribute)descriptorSpectrum.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow =
attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib, false);
}
当我打开显示PropertyGrid的窗口时,一切都按预期进行,即根据某个属性的值显示或隐藏某些值
如果用户在PropertyGrid中更改该值,则会调用代码,但网格不会刷新,并保持修改前的状态
我也尝试过添加
[RefreshProperties(RefreshProperties.All)]
之前的属性可能会引发网格刷新(更改可见性),但它不起作用
我遗漏了什么?应该很棘手。尝试重新分配SelectedObject属性。如果在对象的setter中修改BrowsableAttribute,该怎么做?我不能从那里使用PropertyGrid…没人能帮我?