是否可以在ExtendedToolkit PropertyGrid控件中隐藏/显示属性?

时间:2017-11-02 19:04:21

标签: wpf wpftoolkit

我有一个PropertyGrid控件,它的属性在类中定义,如下所示:

[DisplayName("Display Company Logo")]
[PropertyOrder(5)]
public bool HasLogo { get; set; }

[DisplayName("Logo File Path")]
[PropertyOrder(6)]
[Browsable(true)]
[Editor(typeof(FilePickerEditor), typeof(FilePickerEditor))]
public string LogoFilePath { get; set; }

是否可以隐藏LogoFilePath属性,具体取决于是否选中HasLogo?或者,至少将自定义FilePickerEditor设为只读。

1 个答案:

答案 0 :(得分:0)

我能够使用行为来解决这个问题。 PropertyGrid定义如下:

<toolkitExt:PropertyGrid Tag="DependentVisibility,HasLogo,LogoFilePath"
                         SelectedObject="{Binding PropertyGridSourceObjectUserPreferences, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <i:Interaction.Behaviors>
        <b:PropertyGridBehavior/>
    </i:Interaction.Behaviors>
</toolkitExt:PropertyGrid>

行为:

public class PropertyGridBehavior : Behavior<PropertyGrid>
{
    string _tag;
    List<Tuple<string, string, string>> _dependentVisibilityList;
    PropertyGrid _propertyGrid;

    protected override void OnAttached()
    {
        _propertyGrid = AssociatedObject as PropertyGrid;
        _dependentVisibilityList = new List<Tuple<string, string, string>>();

        if(_propertyGrid.Tag !=null)
        {
            _tag = _propertyGrid.Tag.ToString();

            foreach(var v in _tag.Split(';'))
            {
                if (v.Split(',').Count() != 3) return;
                _dependentVisibilityList.Add(new Tuple<string, string, string>(v.Split(',')[0], v.Split(',')[1], v.Split(',')[2]));
            }
        }

        _propertyGrid.Loaded += _propertyGrid_Loaded;
        _propertyGrid.PropertyValueChanged += PropertyGrid_PropertyValueChanged;
    }

    private void _propertyGrid_Loaded(object sender, RoutedEventArgs e)
    {
        foreach(var v in _propertyGrid.Properties as PropertyItemCollection)
        {
            PropertyItemDependencyVisibilitySet(v.PropertyName, v.Value);
        }
    }

    private void PropertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
    {
        if (e.NewValue.GetType().Equals(typeof(bool)))
        {
            PropertyItem originalSource = (PropertyItem)e.OriginalSource;
            PropertyItemDependencyVisibilitySet(originalSource.PropertyName, (bool)e.NewValue);
        }
    }

    private void PropertyItemDependencyVisibilitySet(string propertyName, object propertyValue)
    {
        try
        {
            Tuple<string, string, string> dependentVisibilityItem = _dependentVisibilityList.Where(x => x.Item1 == "DependentVisibility" && x.Item2 == propertyName).FirstOrDefault();

            if (dependentVisibilityItem != null)
            {
                PropertyItemCollection propertyCollection = _propertyGrid.Properties as PropertyItemCollection;
                PropertyItem propertyItemDestination = propertyCollection.Where(x => x.PropertyName == dependentVisibilityItem.Item3).FirstOrDefault();

                if (propertyItemDestination != null) propertyItemDestination.Visibility = (bool) propertyValue ? Visibility.Visible : Visibility.Collapsed;
            }
        }
        catch
        {
        }
    }
}