1

我有一个具有属性的类,基于在组合框属性中选择一项,将显示或隐藏其他属性。我正在使用 [RefreshProperties(RefreshProperties.All)] 作为组合框属性。绑定到属性网格的类:

[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 属性网格。如果我缺少任何要设置的属性,需要帮助。

4

1 回答 1

3

我在修改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]);
    }
}
于 2015-08-04T14:24:49.993 回答