Home Forums WPF controls Xceed Toolkit Plus for WPF PropertyGrid: how to activate editor for property?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #41790 |

    Hi!

    When I select new object to edit in PropertyGrid, I want preselect one of object’s properties and enter edit mode, so user can immediately typing new value. Is that possible? Can you show example, please?

    Imported from legacy forums. Posted by Vincent (had 221 views)

    Fawzi [Xceed]
    Member
    Post count: 722
    Hi Vincent,
    Yes this is possible.
    The idea is to set the propertyGrid.SelectedProperty to the wanted PropertyItem and then when loaded, focus on its editor.
    Here’s how : 
    private void Button_Click( object sender, RoutedEventArgs e )
        {
          _propertyGrid.SelectedObject = new MyClass() { ID = 31, FirstName = “Carey”, LastName = “Price” };
          this.SelectPropertyItem( 2 );
        }
        private void Button_Click_1( object sender, RoutedEventArgs e )
        {
          _propertyGrid.SelectedObject = new MyClass() { ID = 76, FirstName = “PK”, LastName = “Subban” };
          this.SelectPropertyItem( 1 );
        }
        private void SelectPropertyItem( int index )
        {
          var selectedPropertyItem = _propertyGrid.Properties[ index ] as PropertyItem;
          _propertyGrid.SelectedProperty = selectedPropertyItem;
          selectedPropertyItem.Loaded += this.PropertyItem_Loaded;
        }
        private void PropertyItem_Loaded( object sender, RoutedEventArgs e )
        {
          var item = sender as PropertyItem;
          if( item != null )
          {
            item.Loaded -= this.PropertyItem_Loaded;
            item.Editor.Focus();
          }
        }

    Imported from legacy forums. Posted by Fawzi [Xceed] (had 74 views)

    User (Old forums)
    Member
    Post count: 23064

    Fawzi, thank you! Well, it’s not so trivial code – too complicated for such simple task.

    I tried it, but got interesting error: once I come to edit mode… I cannot leave it! 🙂 I pressed [Enter], [Esc], no effect. Any idea? 

    Imported from legacy forums. Posted by Vincent (had 28 views)

    Fawzi [Xceed]
    Member
    Post count: 722
    Hi,
    This is not an error.
    When editing a PropertyItem, “Enter” or “Escape” won’t leave the edition state. In the propertyGrid, we always want to be in an editing state.
    You can set the PropertyGrid.UpdateTextBoxSourceOnEnterKey to true. This will update the binding on string PropertyItems when an “Enter” is pressed while editing. But you will remain in the edition state for this propertyItem. 
    Other types of propertyItems will update their binding on a selection for comboboxes, checboxes, collectons, Color….or on key pressed for numeric types. You don’t have to press “Enter” to validate those types.
    If you want to put the focus away from the edited propertyItem when “Enter” or “Escape” are pressed, you can catch the “Enter/Esc” key press and focus the PropertyGrid :
    protected override void OnKeyDown( KeyEventArgs e )
        {
          if( (e.Key == Key.Enter) || (e.Key == Key.Escape) )
          {
            _propertyGrid.Focus();
          }
          base.OnKeyDown( e );
        }

    Imported from legacy forums. Posted by Fawzi [Xceed] (had 121 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.