相关疑难解决方法(0)

InputBindings只在聚焦时才起作用

我设计了一个可重复使用的用户控件.它包含UserControl.InputBindings.这很简单,因为它只包含一个标签和一个按钮(和新的属性等)

当我在窗口中使用控件时效果很好.但是密钥绑定仅在集中时才起作用.当一个控件具有对alt + f8的绑定时,此快捷方式仅在聚焦时才有效.当具有自己的绑定的另一个被聚焦时,那个可以工作但不再是alt + f8.当没有控件具有焦点时,没有任何作用.

如何实现我的usercontrol定义窗口范围的键绑定?

特别是遵循MVVM设计模式(Caliburn.Micro使用),但任何帮助表示赞赏.


用户控件的XAML:

<UserControl x:Class="MyApp.UI.Controls.FunctionButton"
             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:MyApp.UI.Controls"
             xmlns:cm="http://www.caliburnproject.org"
             x:Name="Root"
             Focusable="True"
             mc:Ignorable="d" 
             d:DesignHeight="60" d:DesignWidth="120">
    <UserControl.Resources>
        ...
    </UserControl.Resources>
    <UserControl.InputBindings>
        <KeyBinding Key="{Binding ElementName=Root, Path=FunctionKey}" Modifiers="{Binding ElementName=Root, Path=KeyModifiers}" Command="{Binding ElementName=Root, Path=ExecuteCommand}" />
    </UserControl.InputBindings>
    <DockPanel LastChildFill="True">
        <TextBlock DockPanel.Dock="Top" Text="{Binding ElementName=Root, Path=HotkeyText}" />
        <Button DockPanel.Dock="Bottom" Content="{Binding ElementName=Root, Path=Caption}" cm:Message.Attach="[Event Click] = [Action ExecuteButtonCommand($executionContext)]" cm:Action.TargetWithoutContext="{Binding ElementName=Root}" />
    </DockPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

用法示例:

    <Grid>
    <c:FunctionButton Width="75" Height="75" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FunctionKey="F1" ShiftModifier="True" cm:Message.Attach="[Event Execute] = [Action Button1Execute]" />
    <c:FunctionButton Width="75" Height="75" …
Run Code Online (Sandbox Code Playgroud)

wpf user-controls mvvm inputbinding .net-4.5

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net-4.5 ×1

inputbinding ×1

mvvm ×1

user-controls ×1

wpf ×1