62,243
社区成员




<TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OnUserNameKeyDown}" CommandParameter="???" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
public ICommand OnUserNameKeyDown { get; set; }
/// <summary>
/// 绑定事件
/// </summary>
void BindEvent()
{
this.OnUserNameKeyDown = new DelegateCommand<KeyEventArgs>(UserNameKeyDown);
}
void UserNameKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Login(null);
}
}
xmlns:gmc="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
...
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<gmc:EventToCommand PassEventArgsToCommand="True" Command="{Binding KeyDownCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="UserNameKeyDown" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
UserNameKeyDown要改成标准的事件处理方法的形式:
public void UserNameKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Login(null);
}
}
当然模仿InvokeCommandAction自己写一个可以传参的action也很容易:
public class InvokeEventCommand : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InvokeEventCommand));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
protected override void Invoke(object parameter)
{
if (Command != null && Command.CanExecute(parameter))
Command.Execute(parameter);
}
}
前台调用:
<TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<local:InvokeEventCommand Command="{Binding OnUserNameKeyDown}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
还是一种方法是做一个attached propety, 这样可以直接写在TextBox标记里,不必用Interaction.Triggers绕个圈子:
public class CommandAction
{
public static DependencyProperty KeyDownProperty =
DependencyProperty.RegisterAttached("KeyDown", typeof(ICommand), typeof(CommandAction),
new UIPropertyMetadata(KeyDownCommandChanged));
public static void SetKeyDown(UIElement target, bool value)
{
target.SetValue(KeyDownProperty, value);
}
private static void KeyDownCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var textbox = target as TextBox;
if (textbox == null)
throw new InvalidOperationException("This behavior can be attached to TextBox only.");
if (e.NewValue != null && e.OldValue == null)
textbox.KeyDown +=TextboxKeyDown;
else if (e.NewValue == null && e.OldValue != null)
textbox.KeyDown -= TextboxKeyDown;
}
static void TextboxKeyDown(object sender, KeyEventArgs e)
{
var textbox = (TextBox) sender;
var command = (ICommand)textbox.GetValue(KeyDownProperty);
command.Execute(e);
}
}
前台: <TextBox Height="30" Name="txtUserName" Width="160" FontSize="20" VerticalContentAlignment="Center" Text="{Binding UserNme}"
local:CommandAction.KeyDown="{Binding OnUserNameKeyDown}"/>