Telerik PropertyDefinition在运行时应用

时间:2014-03-17 02:16:00

标签: c# telerik

我试图创建一个RadPropertyGrid,它可以在运行时接收一个对象并显示它的属性。我遇到的问题是这些数据是分离的,并且可能有自己的属性定义需要传递到网格中。

有没有办法在C#中执行此操作,以便可以在运行时执行,应用与PropertyGrid无法识别的数据源相关的属性?

提前致谢

1 个答案:

答案 0 :(得分:2)

使用行为。 例如:

在.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
            }
        }
    }