gpt4 book ai didi

c# - 如何在按钮单击时聚焦文本框

转载 作者:行者123 更新时间:2023-11-30 19:42:43 27 4
gpt4 key购买 nike

那么如何使用 MVVM 模式在单击按钮时聚焦文本框?

我基于 this Answer 创建了一个简单的测试项目这适用于第一次点击,但之后它不再设置焦点。我想念什么?

XAML( View )

<Grid>
<TextBox Height='23' HorizontalAlignment='Left' Margin='12,12,0,0' VerticalAlignment='Top' Width='120'
Text='{Binding TheText}'
local:FocusExtension.IsFocused="{Binding IsFocused}"/>
<Button Content='Click' Height='23' HorizontalAlignment='Left' Margin='138,11,0,0' VerticalAlignment='Top' Width='75'
Command='{Binding ClickCommand}'/>
<Button Content='Just to deFocus' Height='28' HorizontalAlignment='Left' Margin='14,44,0,0' Name='button1' VerticalAlignment='Top' Width='199' />
</Grid>

View 模型

public class ViewModel : INotifyPropertyChanged
{
public string TheText { get; set; }
public bool IsFocused { get; set; }

private RelayCommand _clickCommand;
public ICommand ClickCommand
{
get { return _clickCommand ?? (_clickCommand = new RelayCommand(param => this.OnClick())); }
}
private void OnClick()
{
IsFocused = true;
RaisePropertyChanged("IsFocused");
}

#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

public void RaisePropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}

#endregion
}

这是一个 Download link为懒惰的人准备好项目(VS2010);)

最佳答案

在覆盖初始默认值后,您的附加属性值永远不会恢复为 false。因此,您的 FocusExtension 类不会在 TextBox 上调用 Focus(),因为 PropertyChanged 不需要在以下情况下触发将 VM 中的 IsFocused 设置为 true。

切换 OnIsFocusedPropertyChanged(...)

来自:

private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
uie.Focus(); // Don't care about false values.
}

private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var uie = (UIElement)d;
if (!((bool)e.NewValue))
return;
uie.Focus();
uie.LostFocus += UieOnLostFocus;
}

private static void UieOnLostFocus(object sender, RoutedEventArgs routedEventArgs) {
var uie = sender as UIElement;
if (uie == null)
return;
uie.LostFocus -= UieOnLostFocus;
uie.SetValue(IsFocusedProperty, false);
}

更新:

除了上面的变化还要确保

local:FocusExtension.IsFocused="{Binding IsFocused}"

切换到

local:FocusExtension.IsFocused="{Binding IsFocused, Mode=TwoWay}"

Working Download Link

另一个更新

Mode=TwoWay 设置为 FocusExtension 类开关中此附加属性的默认值

public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusExtension),
new UIPropertyMetadata(
false,
OnIsFocusedPropertyChanged));

public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusExtension),
new FrameworkPropertyMetadata(
false,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnIsFocusedPropertyChanged));

您可以跳过使用上述声明在 xaml 中显式指定 Mode=TwoWay

关于c# - 如何在按钮单击时聚焦文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16964886/

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