【问题标题】:WPF Extended Toolkit PropertyGrid: Select propertyWPF 扩展工具包 PropertyGrid:选择属性
【发布时间】:2016-09-01 05:27:38
【问题描述】:

我想将焦点设置在属性网格的第一项上。所以在添加一个对象并将其绑定到 PropertyGrid 之后,您可以更改第一个属性。

我试过了,但是不行:

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

【问题讨论】:

    标签: c# wpf propertygrid


    【解决方案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

    【讨论】:

    • 项目被选中,但我不能直接通过键盘设置值,必须按TAB才能编辑
    • @Suplanus 我已经修改了我的代码以满足您的需求。现在 Value-Box 已聚焦并准备好输入
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多