我有一个由第三方应用程序托管的wpf用户控件作为非模态可停靠窗口。我想在窗口获得焦点或鼠标进入窗口时触发事件。
这是一个MVVM设计。如何将Grid或UserControl的事件处理程序绑定到viewmodel上的方法。我想避免使用xaml.cs代码,因为我必须将datacontext转换为预期的viewmodel来进行调用。
<UserControl x:Class="MVVMWithCommand.ChartExtentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MVVMWithCommand"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" GotFocus="UserControl_GotFocus">
<UserControl.DataContext>
<local:MyViewModel/>
</UserControl.DataContext>
<Grid GotFocus="Grid_GotFocus">
发布于 2018-08-09 15:37:45
你可以通过在XAML中添加以下两行来实现此目的:
xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
你还需要将MvvmLightLibs
Nuget-Explorer添加到项目中。
然后,你可以使用EventToCommand
功能将事件绑定到ViewModel中的命令。
<Grid>
<intr:EventTrigger EventName="GotFocus">
<cmd:EventToCommand Command="{Binding GotFocusCommand}"
PassEventArgsToCommand="True" />
</intr:EventTrigger>
</Grid>
https://stackoverflow.com/questions/-100005944
复制相似问题