如何围绕按钮点击文本框的 [英] How to Focus an TextBox on Button click
问题描述
因此,如何使用MVVM模式的按钮点击焦点的文本框
So how to Focus an TextBox on Button click using the MVVM Pattern?
我基础上的这个答案这适用于第一次点击,但之后不设置对焦了。我怀念什么?
i created an simple Testproject Based on this Answer which works on the first click, but after that it doesn't set the Focus anymore. What do i miss?
XAML(视图)
<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>
视图模型
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
}
和这里是一个具有蓄势待发项目(VS2010的下载链接 )为懒惰的;)
and here is an Download link with an ready to go Project(VS2010) for the lazy ones ;)
推荐答案
初始默认后,您的附加属性值永远不会回false被覆盖。因此,你的 FocusExtension
类不叫焦点()
在文本框
因为的PropertyChanged
并不需要在你的虚拟机真正的 IsFocused
设置时开火。
Your attached property value is never going back to false after the initial default is overwritten. hence your FocusExtension
class is not calling Focus()
on the TextBox
since the PropertyChanged
does not need to fire when setting IsFocused
in your VM to 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.
}
到
to
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);
}
更新:
随着上述变化也确保
地方:FocusExtension.IsFocused ={结合IsFocused}
切换到
地方:FocusExtension.IsFocused ={结合IsFocused,模式=双向}
另一个更新
要设置模式=双向
在 FocusExtension
级交换机默认此附加属性
To set the Mode=TwoWay
as default for this attached property in FocusExtension
class switch
public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusExtension),
new UIPropertyMetadata(
false,
OnIsFocusedPropertyChanged));
到
to
public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusExtension),
new FrameworkPropertyMetadata(
false,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnIsFocusedPropertyChanged));
您可以跳过显式指定模式=双向
在上述声明中的XAML。
You can skip explicitly specifying Mode=TwoWay
in xaml with the above declaration.
这篇关于如何围绕按钮点击文本框的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!