尽管在这个论坛和其他论坛上有一些帖子,但我找不到告诉我如何在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>
你也可以在代码中使用TxtB.Focus()
,如果你不想在XAML中实现这个功能。<TextBox Text="{Binding MyText}" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
我刚开始使用WPF,在阅读上面的例子时,我尝试使用给出的xaml代码示例来设置文本框的焦点,但是以上所有示例都不起作用。
我发现我必须将FocusManager.FocusElement放置在页面元素中。如果您使用窗口作为父元素,这可能也适用。不管怎样,这是对我有效的代码:
<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>
我有一个位于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();
}
}
当文本框中的光标显示时,表示程序已开始运行。我认为要使FocusManager.FocusedElement属性生效,必须将焦点给予FocusScope。
Jerry
Usage:
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);
}
从实验中得知,XAML解决方案
FocusManager.FocusedElement="{Binding ElementName=yourElement}"
在智能感知中找不到FocusManager有点让我困惑。我只是输入了整个属性,然后它就起作用了。
FocusManager.FocusedElement="{Binding ElementName=MyTextBox}"
微软Visual Studio企业版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();
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
expressionTextBox.Focus();
}
FocusManager.FocusedElement= "{Binding ElementName= Comobox1}"
在网格或分组框等中