2016-09-01 18 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)); 
    } 

クロージャ

ここで重要なのは、すべてのプロパティを保持するItemsControlであるPropertyItemsControlを知ることです。

希望すると便利です。

クレジット

Bring ItemsControl into view
Get Nested element from a Control

+0

これは動作しますが、問題はまだある: – Suplanus

+0

項目が選択されているが、私は直接キーボードで値を設定することはできませんので、私がTABキーを押す必要がありました – Suplanus

+0

@Suplanus Iveはあなたのニーズに合わせて自分のコードを変更できます。バリューボックスはフォーカスされ、入力準備が整いました – lokusking

関連する問題