0

Есть главное окно - MainWindow. В него вложена View. И в эту View вложена ещё одна ChildView. Приложение сделано по паттерну MVVM.

Как это выглядит: (MainWindow)

<Window.DataContext>
    <vm:RootVM />
</Window.DataContext>

<ContentPresenter Content="{Binding CurrentContentVM}"/>

View

<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>

ChildView

<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. Но это не сработало. Во View сделана привязка тоже. Но там все работает.

Вопрос

Как сделать, чтобы привязка к клавише в ChildView заработала?

4
  • Это зависит от того, какой контрол в фокусе, наверное. Ваша View может получать фокус вообще? 14 мар 2020 в 7:45
  • @АндрейNOP, да дело в фокусе. Спасибо! Не подскажите как можно установить фокус на контрол после того как его Visiblity стал равен Visible` в рамках MVVM?
    – zodiak1
    14 мар 2020 в 10:24
  • Через ковертер?
    – zodiak1
    14 мар 2020 в 10:25
  • Ну фокус это, по идее, чисто вьюшная задача. VM об этом ничего знать не должна 14 мар 2020 в 14:15

1 ответ 1

1

Проблема была в том, что ChildView не получала фокус.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.