【问题标题】:Focus on a textbox in autocompletebox?专注于自动完成框中的文本框?
【发布时间】:2014-03-11 21:27:50
【问题描述】:

这是我的 xaml:

 <toolkit:AutoCompleteBox Name="signalNameEditor"
                         ItemsSource="{Binding MySource}"
                         SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                         IsTextCompletionEnabled="True"                             
                         FilterMode="StartsWith"
                         ValueMemberPath="Label"
                         MinimumPrefixLength="3"
                         MinimumPopulateDelay="800"
                         Style="{StaticResource autoCompleteBoxStyle}">

    <toolkit:AutoCompleteBox.ItemTemplate>            
        <DataTemplate>                
            <StackPanel>
                <TextBlock Name="textBlock" Text="{Binding Label}"/>
            </StackPanel>
         </DataTemplate>
    </toolkit:AutoCompleteBox.ItemTemplate>        
</toolkit:AutoCompleteBox>

那么,我怎样才能在我的视图中获取 textblock 元素?我试过这个:

var textBlock = signalNameEditor.FindName("textBlock");

但这是错误的。所以你能帮我解决这个问题或将我重定向到一个适当的解决方案。提前致谢。

感谢所有的回答,成功了

 var textBlock = ((StackPanel)signalNameEditor.ItemTemplate.LoadContent()).FindName("textBlock") as TextBlock;

但不幸的是,我没有得到预期的结果。问题是如何将焦点放在自动完成框中的文本框上,这样当焦点在自动完成框上时,我可以在那里写一些东西而无需双击。 我以为我可以在我的视野内做点什么

public void SetFocus { var textBlock = ((StackPanel)signalNameEditor .ItemTemplate .LoadContent()) .FindName("textBlock") as TextBlock;
textBlock.Focus(); }

我知道有很多这样的设置焦点的方法示例 autocompletebox focus in wpf 但我不能让它为我工作。有没有一个解决方案,我可以在不编写 AutoCompleteFocusableBox 类的情况下获得?

【问题讨论】:

  • 你试过 x:Name 吗?有了它,您应该能够通过名称访问它,无需查找或其他任何内容。
  • 无论您在这里尝试做什么,都应该使用适当的 DataBinding 而不是过程代码。
  • ((StackPanel)signalNameEditor.ItemTemplate.LoadContent()).FindName("textBlock") - 我是从内存中写的,所以检查一下
  • 所以 textBlock.BringIntoView() 不起作用?如果没有尝试 x:Name
  • @michael,感谢您的工作,但遗憾的是没有解决我的问题。对不起,不清楚的问题,我只是认为这个技巧会起作用。

标签: c# wpf xaml autocompletebox


【解决方案1】:

解决方案更简单。实际上我需要将焦点设置在自动完成框中的文本框上。为此,我使用了定义为常规样式的样式http://msdn.microsoft.com/ru-ru/library/dd728668(v=vs.95).aspx

在我看来,我可以使用以下内容:

public void SetFocus()
{
        var textbox = this.editor.Template.FindName("Text", editor) as TextBox;
        textbox.Focus();
}

【讨论】:

    【解决方案2】:

    您可以为文本框编写扩展并设置自定义属性以使其具有焦点

    例如你可以编写如下扩展类

    public static class FocusBehavior
    {
        #region Constants
    
        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached("IsFocused", typeof (bool?),
                typeof (FocusBehavior), new FrameworkPropertyMetadata(IsFocusedChanged));
    
        #endregion
    
        #region Public Methods
    
        public static bool GetIsFocused(DependencyObject obj)
        {
            return (bool) obj.GetValue(IsFocusedProperty);
        }
    
        public static void SetIsFocused(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFocusedProperty, value);
        }
    
        #endregion
    
        #region Event Handlers
    
        private static void IsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uie = (UIElement) d;
    
            if ((bool) e.NewValue)
                uie.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() => Keyboard.Focus(uie)));
        }
    
        #endregion Event Handlers
    }
    

    然后在 xaml 中如下:

    <UserControl xmlns:behaviours="clr-namespace:Example.Views.Behaviours">
    
    
    <TextBox TextWrapping="Wrap" Text="TextBox" behaviours:FocusBehavior.IsFocused={Binding IsFocused}/>
    

    希望能回答你的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多