简体   繁体   English

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

[英]Is it possible to hide/show a property in ExtendedToolkit PropertyGrid control?

I have a PropertyGrid control, which has its properties defined in a class, like this: 我有一个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; }

Is it possible to hide LogoFilePath property, depending on whether HasLogo is checked or not? 是否可以根据是否已检查HasLogo来隐藏LogoFilePath属性? Or, at least, make custom FilePickerEditor read-only. 或者至少将自定义FilePickerEditor设置为只读。

I was able to do solve that using a behavior. 我能够使用行为来解决这一问题。 PropertyGrid defined like this: PropertyGrid定义如下:

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

Behavior: 行为:

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
        {
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM