focus on property item

Grids for WPF Forum

Posted 15 years ago by Balaram Barange
Version: 4.5.0487
Avatar
Hi Team,

I want to set focus on 1st item of Property Grid. Please look at following sample, it failed to set focus on property item. How shouid I set focus on that?

        private void Init()
        {
            PropertyGridCategoryItem category = new PropertyGridCategoryItem();
            category.DisplayName = "Person Information";
            myGrid.Items.Add(category);

            PropertyGridPropertyItem item = new PropertyGridPropertyItem();
            item.DisplayName = "Name";
            item.Focusable = true;
            bool retVal = item.Focus(); // It return false.
            category.Accessors.Add(item);
        }

Comments (3)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Balaram,

The PropertyGridPropertyItem and PropertyGridCategoryItem are only used by the data layer. This means that they are not actually presented in the visual tree, and therefore are not focusable. All items in the PropertyGrid are presented by an instance of PropertyGridDataAccessorItem. The only reason we have PropertyGridPropertyItem and PropertyGridCategoryItem derive from FrameworkElement is so they have full support for data bindings.

To set the focus, you would need to wait until the PropertyGrid is loaded *and* the containers for the items have been generated. To make things a bit more complicated, you may also have to wait until the containers for the items under categories have been generated.

The Breadcrumb Intro demo under Navigation includes sample code that shows how to walk a TreeView and select a specific item. Since the PropertyGrid is an extension of the TreeView, the same concepts apply. In your case though, you would stop when you reach the first focusable PropertyGridDataAccessorItem (where DataAccessorType is set to Property).

Hope this helps.


Actipro Software Support

Posted 15 years ago by Balaram Barange
Avatar
Can you please provide sample code for setting focus on property item?
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
I've added a QuickStart for the next maintenance release that shows how to do this. For the interim, the relevant code is:
private bool foundFirstProperty = false;

private void SelectFirstProperty(ItemsControl control) {

    // If another call found the first property, then just exit
    if (this.foundFirstProperty)
        return;

    // If the control has not generated it's containers, then we need to delay our call until it does.
    if (control.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) {

        // Find the current item in the control's Items
        foreach (object item in control.Items) {

            // Get the container for the specified item
            PropertyGridDataAccessorItem container = (PropertyGridDataAccessorItem)control.ItemContainerGenerator.ContainerFromItem(item);
            if (null != container) {
                if (container.DataAccessorType == DataAccessorType.Property) {
                    
                    // This is a property, so focus it
                    container.IsSelected = true;
                    container.BringIntoView();

                    UIElement element = VisualTreeHelperExtended.GetFirstFocusableDescendant(container);
                    if (null != element)
                        element.Focus();

                    this.foundFirstProperty = true;
                    break;
                }
                else if (container.DataAccessorType == DataAccessorType.Category) {
                    // Drill down into categories
                    SelectFirstProperty(container);
                }
            }
        }
    }
    else {
        control.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, (DispatcherOperationCallback)delegate(object arg) {
            SelectFirstProperty(control);
            return null;
        }, null);
    }
}
You would pass the PropertyGrid control in your call to this method and it would then walk the tree looking for the first property.

The QuickStart also shows how to select a property or category by name.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.