gpt4 book ai didi

wpf - 在样式触发器中为文本框设置焦点

转载 作者:行者123 更新时间:2023-12-04 22:35:06 24 4
gpt4 key购买 nike

我有以下内容,但触发器不工作。它应该将光标放在准备好输入的 TextBox 中。

请有人指出我正确的方向。

<Popup Name="PopupGoto" Placement="Mouse" StaysOpen="False" 
IsOpen="{Binding Path=GotoPopupIsOpen, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">

<Popup.Style>
<Style TargetType="Popup">
<Style.Triggers>
<Trigger Property="IsOpen" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=GotoTextbox}" />
</Trigger>
</Style.Triggers>
</Style>
</Popup.Style>

<Border>
<Grid>
<TextBox x:Name="GotoTextbox" Text="{Binding GotoLineNumber}" />
<Button Content="Goto" Command="{Binding GotoCommand}" />
</Grid>
</Border>
</Popup>

最佳答案

FocusManager.FocusedElement为元素建立一个逻辑焦点:

Determines whether the element this property is attached to has logical focus.

这是什么意思?这意味着,在 WPF 中有两个与焦点相关的主要概念:键盘焦点和逻辑焦点 [MSDN] .下面是一个例子:

<TextBox Name="GotoTextbox" 
Text="TestText"
Keyboard.GotKeyboardFocus="GotoTextbox_GotKeyboardFocus"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Mode=Self}}" />

在这种情况下,我们设置了逻辑焦点,但是当您打开 Popup 时,光标呈现并且我们无法打印文本(我建议您自己检查一下)。这也可以使用事件处理程序进行检查 Keyboard.GotKeyboardFocus :

private void GotoTextbox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
MessageBox.Show("Give focus!");
}

当您的Popup 打开时,MessageBox 不会出现,当您按下键盘上的键时,Window 会出现。

为了一切正常工作,需要设置键盘焦点,这也叫物理焦点。我在 Style.Triggers 中看到过很多将 Focus 设置为 TextBox 的示例,但对我来说,出于可以理解的原因,它们并没有起作用。

我可以建议的唯一解决方案 - 是使用附加行为,它非常适合 MVVM 的风格。

焦点行为

public static class FocusBehavior
{
public static readonly DependencyProperty IsFocusProperty;

public static void SetIsFocus(DependencyObject DepObject, bool value)
{
DepObject.SetValue(IsFocusProperty, value);
}

public static bool GetIsFocus(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(IsFocusProperty);
}

static FocusBehavior()
{
IsFocusProperty = DependencyProperty.RegisterAttached("IsFocus",
typeof(bool),
typeof(FocusBehavior),
new UIPropertyMetadata(false, IsFocusTurn));
}

private static void IsFocusTurn(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var element = sender as Control;

if (element != null)
{
if (e.NewValue is bool && (bool)e.NewValue == true)
{
element.Loaded += ElementLoaded;
}
}
}

private static void ElementLoaded(object sender, RoutedEventArgs e)
{
var control = sender as Control;

if (control != null)
{
if (control is TextBox)
{
Keyboard.Focus(control);
}
else
{
control.Focus();
}
}
}
}

在这里,我们为 Control 设置了 Loaded 事件处理程序,并将焦点设置到该元素。使用此行为的示例:

<Grid>
<ToggleButton Name="OpenButton"
Content="Open"
Width="100"
Height="30" />

<Popup Name="PopupGoto"
StaysOpen="False"
PlacementTarget="{Binding ElementName=OpenButton}"
IsOpen="{Binding Path=IsChecked, ElementName=OpenButton}">

<Border>
<StackPanel>
<TextBox x:Name="GotoTextbox"
this:FocusBehavior.IsFocus="True"
Text="TestText" />

<Button Content="Goto" />
</StackPanel>
</Border>
</Popup>
</Grid>

关于wpf - 在样式触发器中为文本框设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com