Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2035
Disable controls at run-time
posted

I want to disable some controls in the XamPropertyGrid when the user makes a selection from other control.  I have tried this two ways'

1. Changing the read-only attribute on the model, for example:

private void setPropertyReadOnly(string property_name, bool read_only)

{
  var attribute = TypeDescriptor.GetProperties(typeof(BraiderSpecModel))[property_name].Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute;
  var field = attribute.GetType().GetField("isReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  field.SetValue(attribute, read_only);
}

2. Getting the control object and disabling it:

    var editor = Utilities.GetDescendantFromName((_property_grid_category), "CarrierSize2") as XamTextEditor;
    if (editor != null)
       editor.IsEnabled = !read_only;

Both of these methods work fine when the control is visible on the screen.  However, if there is a scrollbar on the XamPropertyGrid, then this code has no effect on the XamPropertyGrid.  How can I enable / disable controls in the XamPropertyGridat run-time even when the control is not on the screen?

I am using version 16.1.20161.2134.

Regards,

Arthur

  • 16495
    Offline posted

    Hello Haimai,

    You can create a Dictionary and use it to store the editor's name and the corresponding value for it's IsReadOnly roperty. Then you can create a style, for example for XamTextEditor and handle it's Loaded event by using EventSetter, this way in the event handler you can access the editor from event args. and check whether it exist in your dictionary and depending on the result you can set it's IsReadOnly to  TValue:

    <Style TargetType="{x:Type igEditor:XamTextEditor}" >
        <EventSetter Event="Loaded" Handler="editor_Loaded"/>
    </Style>

    private void editor_Loaded(object sender, RoutedEventArgs e)
    {
        var editor = (sender as XamTextEditor);
        if (controlsToBeDisabled.ContainsKey(editor.Name))
            editor.IsReadOnly = controlsToBeDisabled.Where(ed => ed.Key == editor.Name.ToString()).FirstOrDefault().Value;
    }

    Let me know if you have any questions.

    Sincerely,
    Zhivko
    Associate Software Developer