有一个主窗口 - MainWindow。它有一个嵌套在其中的视图。嵌套在这个View中的是另一个ChildView。该应用程序是根据 MVVM 模式进行的。
它的样子:(MainWindow)
<Window.DataContext>
<vm:RootVM />
</Window.DataContext>
<ContentPresenter Content="{Binding CurrentContentVM}"/>
看法
<UserControl x:Class="SubstationDirectory.V.SubstationEditor"
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:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SubstationDirectory.V"
xmlns:cs="clr-namespace:SubstationDirectory.Converters"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" x:Name="RootElem">
<UserControl.InputBindings>
<KeyBinding Key="Enter" Command="{Binding UpdateSubstation}"/>
</UserControl.InputBindings>
<Grid>
<local:Alert Grid.Row="0" DataContext="{Binding ElementName=RootElem, Path=DataContext.UpdateAlert}" Visibility="{Binding Converter={StaticResource VisibilityFromObjectIsNull}, ConverterParameter=false}" />
</Grid>
子视图
<UserControl x:Class="SubstationDirectory.V.Alert"
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:SubstationDirectory.V"
xmlns:cs="clr-namespace:SubstationDirectory.Converters"
mc:Ignorable="d"
Name="RootElem">
<UserControl.InputBindings>
<KeyBinding Key="Enter" Command="{Binding MainAction}"/>
</UserControl.InputBindings>
</UserControl>
一个任务
您需要将 Enter 按钮绑定到ChildView 命令。
如您所见,我尝试使用InputBindings. 但它没有用。在视图中也进行了绑定。但一切都在那里工作。
问题
如何使ChildView中的键绑定起作用?
问题是ChildView没有获得焦点。