gpt4 book ai didi

c# - ReactiveCommand 传递命令参数

转载 作者:行者123 更新时间:2023-11-30 18:21:39 24 4
gpt4 key购买 nike

我想用command实现textbox的KeyDown事件,

我想让命令可以识别KeyDown事件中的KeyEventArgs之类的键输入并做一些其他事情,

所以我想将命令参数传递给 ReactiveCommand(就像 Eventargs 在事件方法中所做的那样),

但是我不知道怎么做。

我有 WPF Xaml 代码:

<Window x:Class="NameSpaceTest.OpenFileW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
Title="OpenFileWVieModel" Height="50" Width="200" WindowStyle="None">
<Grid>
<TextBox Name="Box" Text="{Binding OpenFPth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OpenFCmd}" CommandParameter=""/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</Grid>
</Window>

两类代码首先是 View ,其次是 View 模型:

   public partial class OpenFileW : Window
{
OpenFileWVieModel OFVM = new OpenFileWVieModel();
public OpenFileW()
{
this.DataContext = OFVM;
InitializeComponent();
}
}


public class OpenFileWVieModel : ReactiveObject
{
public string OpenFPth { get { return _openFpth; } set { this.RaiseAndSetIfChanged(ref _openFpth, value); } }
private string _openFpth;

public IReactiveCommand OpenFCmd { get; set; }

public OpenFileWVieModel()
{
var TextChange = this.WhenAny(
x => x.OpenFPth,
x =>
{
MessageBox.Show("HI");
return true;
});

OpenFCmd = ReactiveCommand.CreateAsyncTask(
TextChange, //CanExecute
async _ =>//Execute
{
MessageBox.Show("HI");
}
);

}
}

下一步如何将文本框 KeyInput 作为参数传递给命令?

最佳答案

您可以自行传递文本框或仅传递文本。我不知道你想用它做什么,但如果你必须与文本框交互,那么我建议你自己传递文本框:

传递文本:

<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OpenFCmd}"
CommandParameter="{Binding ElementName=Box, Path=Text}" />
</i:EventTrigger>
</i:Interaction.Triggers>

传递文本框:

<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OpenFCmd}"
CommandParameter="{Binding ElementName=Box}" />
</i:EventTrigger>
</i:Interaction.Triggers>

编辑:

使用上面的代码,您可以在代码中创建一个事件处理程序:

void OpenFCmn_OnClick(object obj)
{
var tb = (TextBox) obj;

tb.KeyDown += TbOnKeyDown;

}

private void TbOnKeyDown(object sender, KeyEventArgs keyEventArgs)
{
MessageBox.Show(keyEventArgs.Key.ToString());
keyEventArgs.Handled = true;

}

关于c# - ReactiveCommand 传递命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146590/

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