我有一个具有属性的类,基于在combobox属性中选择一个项目,将显示或隐藏其他属性。我使用[RefreshProperties(RefreshProperties.All)]作为combobox属性。 绑定到属性网格的类:
[TypeConverter(typeof(PropertySubsetConverter<FileSystemOperation>))]
public class FileSystemOperation : IPropertySubsetObject
{
[Description("File system operations like Copy, Move, Delete & Check file.")]
[Category("Mandatory")]
[RefreshProperties(RefreshProperties.All)]
public Op Operation { get; set; }
public enum Op
{
/// <summary>
/// Copy file
/// </summary>
CopyFile,
/// <summary>
/// Move file
/// </summary>
MoveFile,
/// <summary>
/// Delete file
/// </summary>
DeleteFile,
/// <summary>
/// Delete directory
/// </summary>
DeleteDirectory,
/// <summary>
/// Check if file exists
/// </summary>
ExistFile
}
}
如果用户选择“DeleteDirectory”,则应显示以下属性,并隐藏其他属性
[AppliesTo(Op.DeleteDirectory)]
public bool Recursive { get; set; }
我的Xaml:
<xctk:PropertyGrid x:Name="pk" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontWeight="ExtraBold" IsEnabled="{Binding PropertyGridIsEnabled}" SelectedObject="{Binding SelectedElement, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="#FF4A5D80" Foreground="White"/>
这适用于Winform属性网格,但不适用于Xceed wpf属性网格。如果我遗漏任何要设置的属性,需要帮助。
答案 0 :(得分:1)
修改属性的ReadOnly
属性时遇到同样的问题。 WinForm PropertyGrid工作,Xceed PropertyGrid没有。可能是付费Plus版本可行。它具有DependsOn
属性。
我使用PropertyGrid的PropertyValueChanged
事件解决了它。
private void propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
// get the descriptor of the changed property
PropertyDescriptor propDesc = ((PropertyItem)e.OriginalSource).PropertyDescriptor;
// try to get the RefreshPropertiesAttribute
RefreshPropertiesAttribute attr
= (RefreshPropertiesAttribute)propDesc.Attributes[typeof(RefreshPropertiesAttribute)];
// if the attribute exists and it is set to All
if (attr != null && attr.RefreshProperties == RefreshProperties.All)
{
// invoke PropertyGrid.UpdateContainerHelper
MethodInfo updateMethod = propertyGrid.GetType().GetMethod(
"UpdateContainerHelper", BindingFlags.NonPublic | BindingFlags.Instance);
updateMethod.Invoke(propertyGrid, new object[0]);
}
}