2016-09-01 135 views
0

我想将焦点设置在属性网格的第一个项目。因此,在添加一个对象并将其绑定到PropertyGrid后,您可以更改第一个属性。WPF扩展工具包的PropertyGrid:选择房产

我尝试这样做,但不工作:

propertyGrid.Focus(); 
propertyGrid.SelectedProperty = propertyGrid.Properties[0]; 

回答

1

遗憾的是,似乎是没有内置解决这个。

我建议的解决方案更像是一个解决办法,但如果它被隐藏代码上设置应适当视觉 - 选择SelectedProperty

首先,我们需要一些扩展

public static class Extensions { 
    public static T GetDescendantByType<T>(this Visual element) where T : class { 
     if (element == null) { 
     return default(T); 
     } 
     if (element.GetType() == typeof(T)) { 
     return element as T; 
     } 
     T foundElement = null; 
     if (element is FrameworkElement) { 
     (element as FrameworkElement).ApplyTemplate(); 
     } 
     for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { 
     var visual = VisualTreeHelper.GetChild(element, i) as Visual; 
     foundElement = visual.GetDescendantByType<T>(); 
     if (foundElement != null) { 
      break; 
     } 
     } 
     return foundElement; 
    } 

    public static void BringItemIntoView(this ItemsControl itemsControl, object item) { 
     var generator = itemsControl.ItemContainerGenerator; 

     if (!TryBringContainerIntoView(generator, item)) { 
     EventHandler handler = null; 
     handler = (sender, e) => 
     { 
      switch (generator.Status) { 
      case GeneratorStatus.ContainersGenerated: 
       TryBringContainerIntoView(generator, item); 
       break; 
      case GeneratorStatus.Error: 
       generator.StatusChanged -= handler; 
       break; 
      case GeneratorStatus.GeneratingContainers: 
       return; 
      case GeneratorStatus.NotStarted: 
       return; 
      default: 
       break; 
      } 
     }; 

     generator.StatusChanged += handler; 
     } 
    } 

    private static bool TryBringContainerIntoView(ItemContainerGenerator generator, object item) { 
     var container = generator.ContainerFromItem(item) as FrameworkElement; 
     if (container != null) { 
     container.BringIntoView(); 
     return true; 
     } 
     return false; 
    } 

    } 

在此之后,你可以很容易地做到以下几点:

//Register to the SelectedPropertyItemChanged-Event 
this._propertyGrid.SelectedPropertyItemChanged += this.PropertyGridSelectedPropertyItemChanged; 
//Set any Property by index 
this._propertyGrid.SelectedProperty = this._propertyGrid.Properties[3]; 

最后做的神奇突出

private void PropertyGridSelectedPropertyItemChanged(object sender, RoutedPropertyChangedEventArgs<PropertyItemBase> e) { 
     var pic = this._propertyGrid.GetDescendantByType<PropertyItemsControl>(); 
     pic.BringItemIntoView(e.NewValue); 
     // UPDATE -> Move Focus to ValueBox 
     FocusManager.SetFocusedElement(pic,e.NewValue); 
     var xx = Keyboard.FocusedElement as UIElement; 
     xx?.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
    } 

关闭

这里的一切的关键是,知道PropertyItemsControl这是一个ItemsControl控制所有的属性。

希望这会有所帮助!

现金

Bring ItemsControl into view
Get Nested element from a Control

+0

这工作,但仍有一个问题: – Suplanus

+0

该项目被选中,但我不能直接设置由键盘上输入数值,不得不按TAB键,所以我可以编辑 – Suplanus

+0

@Suplanus伊夫修改我的代码以符合您的需求。现在价值箱已经集中并准备好输入 – lokusking

相关问题