This is a migrated thread and some comments may be shown as answers.

Read-Only property

2 Answers 77 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
AvgurD
Top achievements
Rank 1
AvgurD asked on 28 Oct 2011, 05:53 AM
Hello!

How about read-only property in PropertyGrid? I find only one way - custom editor, like this
 
<telerik:PropertyDefinition DisplayName="My property" Description="My description"
                                                    GroupName="Group name">
                            <telerik:PropertyDefinition.EditorTemplate>
                                <DataTemplate>
                                    <sdk:Label Content="{Binding Geo_Num}" FontWeight="Bold"/>
                                </DataTemplate>
                            </telerik:PropertyDefinition.EditorTemplate>
                        </telerik:PropertyDefinition>

But in this case, the property will be unselectable, and description invisible.
Variant with TextBox in DataTemplate (and ReadOnly="True") fix it - but it looks like editable (frame, backgroudnd, cursor inside, etc.)

2 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 02 Nov 2011, 01:39 AM
Hello Jah,

Thank you for the feedback. We do plan providing read-only support for RadPropertyGrid's default editors in the near future. However, with the current version of RadControls, you will have to set an appropriate DataTemplate, in order to achieve this. Please, do not hesitate to contact us immediately if any further inquiries occur.

Greetings,
Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
Adrian
Top achievements
Rank 1
answered on 16 Nov 2011, 09:45 PM
Hi, meanwhile you implement a property for readonly, I developed a small behavior that allows to set readonly properties from a collection which contains the name of the properties to set as RO.

it requires you bind the IsDisabledCollection property to an ObservableCollection<string> and then add every property name to set as readonly to that collection
               
<behaviors:RadPropertyGridIsPropertyEnabledBehavior IsDisabledCollection="{Binding DisableProperties}"/>

As I can't attach the file, I'm copying the source code of the behavior here. Maybe there could be some optimization in the refresh on collection changed, any suggestion welcome:
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Data.PropertyGrid;
 
 
namespace SmartCost.ClassLibrary.Behaviors
{
    public class RadPropertyGridIsPropertyEnabledBehavior : Behavior<RadPropertyGrid>
    {
        ObservableCollection<string> _isDisabledCollection;
        protected override void OnAttached()
        {
            base.OnAttached();
            (this.AssociatedObject as RadPropertyGrid).Loaded += new RoutedEventHandler(targetObject_Loaded);
 
        }
 
        void targetObject_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshDisabled();
        }
        void RefreshDisabled()
        {
            if (_isDisabledCollection == null)
                return;
            var targetObject = this.AssociatedObject as RadPropertyGrid;
            var children = targetObject.ChildrenOfType<PropertyGridField>();
            foreach (var child in children)
            {
                var enabled = true;
                var binding = ((PropertyDefinition)(child.DataContext)).Binding;
                if (binding!=null && _isDisabledCollection.Contains(binding.Path.Path))
                    enabled = false;
 
                var control = child.Content as Control;
                if (control != null)
                    control.IsEnabled = enabled;
            }
        }
 
        public ObservableCollection<string> IsDisabledCollection
        {
            get { return (ObservableCollection<string>)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }
 
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached(
                "IsDisabledCollection", typeof(ObservableCollection<string>), typeof(RadPropertyGridIsPropertyEnabledBehavior), new System.Windows.PropertyMetadata(OnIsDisabledCollectionChanged));
 
        private static void OnIsDisabledCollectionChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            (s as RadPropertyGridIsPropertyEnabledBehavior).OnIsDisabledCollectionChanged(e);
             
        }
        private void OnIsDisabledCollectionChanged(DependencyPropertyChangedEventArgs e)
        {
            if(_isDisabledCollection!=null)
                _isDisabledCollection.CollectionChanged-=_isDisabledCollection_Changed;
            _isDisabledCollection = e.NewValue as ObservableCollection<string>;
            _isDisabledCollection.CollectionChanged += _isDisabledCollection_Changed;
 
            RefreshDisabled();
        }
        private void _isDisabledCollection_Changed(object s, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RefreshDisabled();
        }
    }
 
}

Hope this helps

Regards
Adrián
Tags
PropertyGrid
Asked by
AvgurD
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Adrian
Top achievements
Rank 1
Share this question
or