我目前有以下代码来在网格中定义TextBox(网格是根据数据填充的,因此也在DataTemplate内部):
<TextBox
MinWidth="120"
Text="{Binding BatchNumber, Mode=TwoWay}"
ToolTip="Redacted"
IsTabStop="True"
MaxLength="32"
<!-- LostFocus="TextBox_LostFocus" -->
/>
当文本框失去焦点时,我想在ViewModel中运行一个函数。使用LostFocus属性并绑定到函数名称会导致运行时错误:
只能在DependencyObject的DependencyProperty上设置“绑定”
因此,我正在寻找有关失去焦点时如何运行ViewModel函数的详细信息。
答案 0 :(得分:0)
在您的ViewModel中尝试添加:
//Public property
public ICommand MyCommand { get; set; }
//In the constructor
MyCommand = new RelayCommand(DoSometing);
//Private method to handle lost focus
private void DoSometing(){
//Do someting
}
然后在您的xaml中调用MyCommand。
LostFocus="{Binding MyCommand}"