【问题标题】:WPF Grid and focus of controls inside itWPF Grid 及其内部控件的焦点
【发布时间】:2012-03-21 13:47:10
【问题描述】:

我有 MyContainer 用户控件,里面有 Grid。此 Grid 的每个单元格都包含一个派生自 MyControlBase 类的一些控件。这些控件是动态添加的。

我需要在 MyContainer 中实现一个 FocusedControl 绑定属性以获取当前焦点或将焦点设置到任何 MyControlBase 子级。我知道 FocusManager.FocusedElement,但不知道如何正确实现它。

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    不知道这是否对您的情况有所帮助,或者它是否是解决我的问题的正确方法,但在紧要关头,我发现在我想要关注的对象上侦听 Loaded 事件,然后使用 Dispatcher.BeginInvoke 来当其他方法没有工作时,向我的后台或 ApplicationIdle 优先级的控件发送一个焦点请求。例如:

    private void MyControlLoaded(object sender, RoutedEventArgs e)
    {
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action( () => MyControl.Focus() ));
    }
    

    【讨论】:

    • 谢谢,我知道如何让任何元素集中 :) 我需要一个漂亮的 set/get 属性来动态控制它以绑定到其他控件。
    【解决方案2】:

    好的,我自己找到了方法。

    首先像往常一样定义我们的新依赖属性FocusedAdapterProperty

        public static readonly DependencyProperty FocusedAdapterProperty;
    
        static SpreadGridControl()
        {
            FocusedAdapterProperty = DependencyProperty.Register("FocusedAdapter",
                                typeof(object), typeof(SpreadGridControl),
                                new FrameworkPropertyMetadata(null, null));
        }
    
        public object FocusedAdapter
        {
            get { return GetValue(FocusedAdapterProperty); }
            set { SetValue(FocusedAdapterProperty, value); }
        }
    

    接下来将 GotFocus 处理程序添加到父容器,例如<Grid GotFocus="Grid_OnGotFocus">
    检查 e.OriginalSource 并搜索所需类型的最常见祖先并将属性设置为新值:

        private void Grid_OnGotFocus(object sender, RoutedEventArgs e)
        {
            var control = UIHelpers.TryFindParent<ControlBase>
                ((DependencyObject)e.OriginalSource);
            if (control != null)
                FocusedAdapter = control.Adapter;
        }
    

    TryFindParent的实现可以参考here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多