wpf 在 TextBox 中输入时执行 viewmodels 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3413927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing viewmodels command on enter in TextBox
提问by Marks
I want to execute a command in my viewmodel when the user presses enter in a TextBox. The command works when bound to a button.
当用户在 TextBox 中按下 Enter 键时,我想在我的视图模型中执行一个命令。该命令在绑定到按钮时起作用。
<Button Content="Add" Command="{Binding Path=AddCommand}" />
But I can't bring it to work from the TextBox. I tried an Inputbinding, but it didn't work.
但是我无法从 TextBox 中使用它。我尝试了 Inputbinding,但没有用。
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=AddCommand}" Key="Enter"/>
</TextBox.InputBindings>
I also tried to set the working button as default, but it doesn't get executed when enter is pressed.
我还尝试将工作按钮设置为默认值,但在按下 Enter 时不会执行它。
Thanks for your help.
谢谢你的帮助。
回答by mkamioner
I know I am late to the party, but I got this to work for me. Try using Key="Return"
instead of Key="Enter"
我知道我参加聚会迟到了,但我得到了这个对我来说有效。尝试使用Key="Return"
代替Key="Enter"
Here is the full example
这是完整的例子
<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Command="{Binding AddCommand}" Key="Return" />
</TextBox.InputBindings>
</TextBox>
Make sure to use UpdateSourceTrigger=PropertyChanged
in your binding, otherwise the property will not be updated until focus is lost, and pressing enter will not lose focus...
确保UpdateSourceTrigger=PropertyChanged
在你的绑定中使用,否则属性不会在失去焦点之前更新,并且按下回车不会失去焦点......
Hope this was helpful!
希望这是有帮助的!
回答by Andreas Zita
You have probably not made the command a property, but a field. It only works to bind to properties. Change your AddCommand to a property and it will work. (Your XAML works fine for me with a property instead of a field for the command -> no need for any code behind!)
您可能没有将命令设为属性,而是将其设为字段。它仅适用于绑定到属性。将您的 AddCommand 更改为一个属性,它将起作用。(您的 XAML 使用属性而不是命令字段对我来说很好用 -> 不需要任何背后的代码!)
回答by Mark Heath
Here's an attached dependency property I created for this. It has the advantage of ensuring that your text binding is updated back to the ViewModel before the command fires (useful for silverlight which doesn't support the property changed update source trigger).
这是我为此创建的附加依赖项属性。它的优点是确保您的文本绑定在命令触发之前更新回 ViewModel(对于不支持属性更改更新源触发器的 Silverlight 很有用)。
public static class EnterKeyHelpers
{
public static ICommand GetEnterKeyCommand(DependencyObject target)
{
return (ICommand)target.GetValue(EnterKeyCommandProperty);
}
public static void SetEnterKeyCommand(DependencyObject target, ICommand value)
{
target.SetValue(EnterKeyCommandProperty, value);
}
public static readonly DependencyProperty EnterKeyCommandProperty =
DependencyProperty.RegisterAttached(
"EnterKeyCommand",
typeof(ICommand),
typeof(EnterKeyHelpers),
new PropertyMetadata(null, OnEnterKeyCommandChanged));
static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
ICommand command = (ICommand)e.NewValue;
FrameworkElement fe = (FrameworkElement)target;
Control control = (Control)target;
control.KeyDown += (s, args) =>
{
if (args.Key == Key.Enter)
{
// make sure the textbox binding updates its source first
BindingExpression b = control.GetBindingExpression(TextBox.TextProperty);
if (b != null)
{
b.UpdateSource();
}
command.Execute(null);
}
};
}
}
You use it like this:
你像这样使用它:
<TextBox
Text="{Binding Answer, Mode=TwoWay}"
my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"/>
回答by Ashot Muradian
You need to define Gesture instead of Key property of the KeyBinding:
您需要定义 Gesture 而不是 KeyBinding 的 Key 属性:
<TextBox.InputBindings>
<KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/>
</TextBox.InputBindings>
回答by T.Y. Kucuk
In addition to Mark Heath's answer, I took the class one step further by implementing Command Parameter attached property in this way;
除了Mark Heath的回答之外,我通过以这种方式实现 Command Parameter 附加属性使课程更进了一步;
public static class EnterKeyHelpers
{
public static ICommand GetEnterKeyCommand(DependencyObject target)
{
return (ICommand)target.GetValue(EnterKeyCommandProperty);
}
public static void SetEnterKeyCommand(DependencyObject target, ICommand value)
{
target.SetValue(EnterKeyCommandProperty, value);
}
public static readonly DependencyProperty EnterKeyCommandProperty =
DependencyProperty.RegisterAttached(
"EnterKeyCommand",
typeof(ICommand),
typeof(EnterKeyHelpers),
new PropertyMetadata(null, OnEnterKeyCommandChanged));
public static object GetEnterKeyCommandParam(DependencyObject target)
{
return (object)target.GetValue(EnterKeyCommandParamProperty);
}
public static void SetEnterKeyCommandParam(DependencyObject target, object value)
{
target.SetValue(EnterKeyCommandParamProperty, value);
}
public static readonly DependencyProperty EnterKeyCommandParamProperty =
DependencyProperty.RegisterAttached(
"EnterKeyCommandParam",
typeof(object),
typeof(EnterKeyHelpers),
new PropertyMetadata(null));
static void OnEnterKeyCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
ICommand command = (ICommand)e.NewValue;
Control control = (Control)target;
control.KeyDown += (s, args) =>
{
if (args.Key == Key.Enter)
{
// make sure the textbox binding updates its source first
BindingExpression b = control.GetBindingExpression(TextBox.TextProperty);
if (b != null)
{
b.UpdateSource();
}
object commandParameter = GetEnterKeyCommandParam(target);
command.Execute(commandParameter);
}
};
}
}
Usage:
用法:
<TextBox Text="{Binding Answer, Mode=TwoWay}"
my:EnterKeyHelpers.EnterKeyCommand="{Binding SubmitAnswerCommand}"
my:EnterKeyHelpers.EnterKeyCommandParam="your parameter"/>