【发布时间】:2011-02-21 18:27:02
【问题描述】:
尽管这个论坛和其他一些帖子我找不到告诉我如何将焦点设置在TextBox 上的内容。
我有一个带有许多标签和文本框的用户控件。加载表单时,我希望特定的文本框具有焦点。
我已经设置了 tabIndex,但这似乎不起作用。
有什么建议吗?
【问题讨论】:
尽管这个论坛和其他一些帖子我找不到告诉我如何将焦点设置在TextBox 上的内容。
我有一个带有许多标签和文本框的用户控件。加载表单时,我希望特定的文本框具有焦点。
我已经设置了 tabIndex,但这似乎不起作用。
有什么建议吗?
【问题讨论】:
您可以为此目的使用FocusManager.FocusedElement 附加属性。这是一段默认将焦点设置为 TxtB 的代码。
<StackPanel Orientation="Vertical" FocusManager.FocusedElement="{Binding ElementName=TxtB}">
<TextBox x:Name="TxtA" Text="A" />
<TextBox x:Name="TxtB" Text="B" />
</StackPanel>
如果您不想在 XAML 中这样做,也可以在代码隐藏中使用 TxtB.Focus()。
【讨论】:
FocusManager 的地方)。最后,我在代码隐藏中做到了。
您可以直接在 TextBox 上应用此属性:
<TextBox Text="{Binding MyText}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
【讨论】:
将要指向焦点的元素绑定为
FocusManager.FocusedElement= "{Binding ElementName= Comobox1}"
在网格或组框等中
【讨论】:
Nov 11 '14”。在亚当发表评论之前,他早已离开 :)
FocusManager 没有智能感知,这让我有点困惑。我刚刚输入了整个属性,它就起作用了。
FocusManager.FocusedElement="{Binding ElementName=MyTextBox}"
Microsoft Visual Studio Enterprise 2015 版本 14.0.23107.0/C#/WPF
【讨论】:
为了完整起见,还有一种方法可以从后面的代码中处理此问题(例如,在控件的情况下,无论出于何种原因,这些控件都是动态创建的,并且在 XAML 中不存在)。将处理程序附加到窗口的 Loaded 事件,然后使用所需控件的“.Focus()”方法。下面是简单的示例。
public class MyWindow
{
private VisualCollection controls;
private TextBox textBox;
// constructor
public MyWindow()
{
controls = new VisualCollection(this);
textBox = new TextBox();
controls.Add(textBox);
Loaded += window_Loaded;
}
private void window_Loaded(object sender, RoutedEventArgs e)
{
textBox.Focus();
}
}
【讨论】:
我是使用 WPF 的新手,阅读上述示例时我也有类似的经历,尝试使用给出的 xaml 代码示例将焦点设置到文本框,即上述所有示例均无效。
我发现我必须将 FocusManager.FocusElement 放在页面元素中。我认为如果您使用 Window 作为父元素,这可能也会起作用。无论如何,这是对我有用的代码。
<Page x:Class="NameOfYourClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="Title"
Height="720"
Width="915"
Background="white"
Loaded="pgLoaded"
FocusManager.FocusedElement="{Binding ElementName=NameOfYourTextBox}">
<!-- Create child elements here. -->
</Page>
【讨论】:
通过试验,xaml 解决方案
FocusManager.FocusedElement="{Binding ElementName=yourElement}"
当您将其放置在窗口层次结构中的最高元素(通常是 Window 或放置其他所有内容的 Grid)中时,似乎效果最佳
【讨论】:
用法:
local:FocusManager.FocusOnLoad="True"
public class FocusManager
{
public static readonly DependencyProperty FocusOnLoad = DependencyProperty.RegisterAttached(
"FocusOnLoad",
typeof(bool),
typeof(FocusManager),
new UIPropertyMetadata(false, new PropertyChangedCallback(OnValueChanged))
);
private static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (!(sender is Control control))
return;
if ((bool) e.NewValue == false)
return;
control.Loaded += (s, e) => control.Focus();
}
public static bool GetFocusOnLoad(DependencyObject d) => (bool) d.GetValue(FocusOnLoad);
public static void SetFocusOnLoad(DependencyObject d, bool value) => d.SetValue(FocusOnLoad, value);
}
【讨论】:
我在 DataTemplate 内的 Grid 中有一个 TextBox,我希望在它可见时获得键盘焦点。我也发现了
<DataTemplate x:Key="DistanceView" DataType="{x:Type vm:ROI}">
<Grid FocusManager.FocusedElement="{Binding ElementName=tbDistance}">
<TextBox x:Name="tbDistance" Grid.Column="1" Grid.Row="1" VerticalAlignment="Bottom"/>
</Grid>
</DataTemplate>
对我不起作用。
但是当我在父 ContentControl 中调用 Focus() 时
private void ContentControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((sender as ContentControl).IsVisible)
{
(sender as ContentControl).Focus();
}
}
它开始工作并且插入符号在文本框中可见。我认为 FocusScope 必须获得焦点才能使 FocusManager.FocusedElement 属性产生任何效果。
杰瑞
【讨论】: