【问题标题】:Is it possible to hide/show a property in ExtendedToolkit PropertyGrid control?是否可以在 ExtendedToolkit PropertyGrid 控件中隐藏/显示属性?
【发布时间】:2017-11-02 19:04:21
【问题描述】:

我有一个 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 设为只读。

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    我能够使用行为来解决这个问题。 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
            {
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多