Telerik PropertyDefinition在运行时应用

本文关键字:应用 运行时 PropertyDefinition Telerik | 更新日期: 2023-09-27 17:50:36

我试图使一个RadPropertyGrid,可以采取在运行时的对象,并显示它的属性。我遇到的问题是,这些数据是解耦的,可能有自己的属性定义,需要传递到网格中。

是否有一种方法可以在c#中做到这一点,以便它可以在运行时执行,应用与PropertyGrid无法知道的数据源相关的属性?

Thanks in advance

Telerik PropertyDefinition在运行时应用

使用行为。例如:

在.xaml:

    <UserControl.Resources>
                <x:Array x:Key="HiddenProperties" Type="sys:String">
                    <sys:String>CanEdit</sys:String>
                    <sys:String>IsEdit</sys:String>
                    <sys:String>DisplayName</sys:String>
                    <sys:String>Changed</sys:String>
                    <sys:String>Changing</sys:String>
                </x:Array>
    </UserControl.Resources>
...
    <telerik:RadPropertyGrid AutoGeneratePropertyDefinitions="True"
                             EditorTemplateSelector="{StaticResource CustomPropertyGridDataTemplateSelector}">
                        <i:Interaction.Behaviors>
                            <extensions:PropertyDefinitionFiltering HiddenProperties="{StaticResource HiddenProperties}" />
                        </i:Interaction.Behaviors>
                    </telerik:RadPropertyGrid>
在cs

    public class PropertyDefinitionFiltering : Behavior<RadPropertyGrid>
    {
        private static RadPropertyGrid _PropertyGrid;
        #region HiddenProperties
        private static string[] _HiddenProperties;
        public static readonly DependencyProperty HiddenPropertiesProperty =
            DependencyProperty.RegisterAttached("HiddenProperties", typeof(string[]),
                                                typeof(PropertyDefinitionFiltering),
                                                new PropertyMetadata(OnSetHiddenProperties));
        public static string[] GetHiddenProperties(DependencyObject obj)
        {
            return (string[])obj.GetValue(HiddenPropertiesProperty);
        }
        public static void SetHiddenProperties(DependencyObject obj, string[] value)
        {
            obj.SetValue(HiddenPropertiesProperty, value);
        }
        private static void OnSetHiddenProperties(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            _HiddenProperties = (string[])e.NewValue;
            var propertyGrid = d as RadPropertyGrid;
            if (null == propertyGrid) return;
            _PropertyGrid = propertyGrid;
        }
        #endregion
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded -= OnLoaded;
            AssociatedObject.Loaded += OnLoaded;
            AssociatedObject.Unloaded -= OnUnloaded;
            AssociatedObject.Unloaded += OnUnloaded;
        }
        protected override void OnDetaching()
        {
            AssociatedObject.Loaded -= OnLoaded;
            OnUnloaded();
            base.OnDetaching();
        }
        protected void OnUnloaded(object sender, RoutedEventArgs eventArgs)
        {
            OnUnloaded();
        }
        protected void OnUnloaded()
        {
            _PropertyGrid.AutoGeneratingPropertyDefinition -= PropertyGridOnAutoGeneratingPropertyDefinition;
        }
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _PropertyGrid = AssociatedObject;
            _PropertyGrid.AutoGeneratingPropertyDefinition -= PropertyGridOnAutoGeneratingPropertyDefinition;
            _PropertyGrid.AutoGeneratingPropertyDefinition += PropertyGridOnAutoGeneratingPropertyDefinition;
        }
        private static void PropertyGridOnAutoGeneratingPropertyDefinition(object sender, AutoGeneratingPropertyDefinitionEventArgs eventArgs)
        {
            PropertyDefinition propertyDefinition = eventArgs.PropertyDefinition;
            string propertyName = propertyDefinition.SourceProperty.Name;
            if (Array.IndexOf(_HiddenProperties, propertyDefinition.AutoGeneratedPath) > 0)
            {
                eventArgs.Cancel = true;
            }
            else
            {
                var descriptor = ((MemberDescriptor)(propertyDefinition.SourceProperty.Descriptor));
                /// Work with properties attribute, properties display name, etc
            }
        }
    }
相关文章: