举例:比如说我在前台有一个DataGrid控件, 在输入数据的时候逻辑焦点现在位于该控件的输入行内,现在我想在鼠标移出控件的时候(MouseLeave)触发,使之失去焦点
解决思路:在View中新建一个TextBox控件,宽度设为0,也就是相当于不占位不显示,当(MouseLeave)触发时,将逻辑焦点转移到该控件上。此时Datagrid就会失去逻辑焦点。
问题在于在ViewModel层,采用常规办法无法直接获取前台控件(如:“控件名. 属性” 的形式)
解决办法:
View下:
在该项目下引用该程序集,用于触发器
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<TextBox x:Name="TextBox1" Width="0"/>
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeave">
<i:InvokeCommandAction Command="{Binding MouCommand}"
CommandParameter="{Binding ElementName=TextBox1}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
ViewModel下:
private DelegateCommand<TextBox > MouCommand; //申明委托
MouCommand= new DelegateCommand<TextBox >(DataGridLoadMouseLeave); //实例化
private void DataGridLoadMouseLeave(TextBox textBox )
{
textBox.Focus(); ;//使用
//当然,你也可以在这设置该控件的属性
}
至此实现该功能