Read-Only property

3 posts, 0 answers
  1. AvgurD
    AvgurD avatar
    18 posts
    Member since:
    Oct 2012

    Posted 28 Oct 2011 Link to this post

    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. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 01 Nov 2011 Link to this post

    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 >>
  3. DevCraft banner
  4. Adrian
    Adrian avatar
    4 posts
    Member since:
    Mar 2011

    Posted 16 Nov 2011 Link to this post

    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
Back to Top