簡體   English   中英

是否可以在ExtendedToolkit PropertyGrid控件中隱藏/顯示屬性?

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

我有一個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; }

是否可以根據是否已檢查HasLogo來隱藏LogoFilePath屬性? 或者至少將自定義FilePickerEditor設置為只讀。

我能夠使用行為來解決這一問題。 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
        {
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM