标签: inputbinding

当TextBox具有焦点时,UserControl中的KeyBinding不起作用

以下情况.我有一个带有五个键绑定的UserControl.当TextBox具有焦点时,UserControl的键绑定将停止触发..

有没有办法解决这个'问题'?

<UserControl.InputBindings>
    <KeyBinding Key="PageDown" Modifiers="Control" Command="{Binding NextCommand}"></KeyBinding>
    <KeyBinding Key="PageUp" Modifiers="Control" Command="{Binding PreviousCommand}"></KeyBinding>
    <KeyBinding Key="End" Modifiers="Control"  Command="{Binding LastCommand}"></KeyBinding>
    <KeyBinding Key="Home" Modifiers="Control" Command="{Binding FirstCommand}"></KeyBinding>
    <KeyBinding Key="F" Modifiers="Control" Command="{Binding SetFocusCommand}"></KeyBinding>
</UserControl.InputBindings>
<TextBox Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Gesture="Enter" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl }}, Path=DataContext.FilterCommand}"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

似乎功能键(F1等)和ALT+ [key]工作.我假设CTRLSHIFT修饰符以某种方式"阻止"事件冒泡到UserControl.

wpf xaml focus mvvm inputbinding

22
推荐指数
3
解决办法
2万
查看次数

在样式中定义InputBindings

我正在使用MVVM设计模式创建一个WPF应用程序,我正在尝试扩展TabItem控件,以便在用户单击鼠标中键时关闭选项卡.我正在尝试使用InputBindings实现这一点,并且在我尝试在样式中定义它之前它非常有效.我已经了解到,除非使用DependencyProperty附加,否则无法将InputBindings添加到样式中.所以我在这里跟着这个类似的帖子......它几乎可以工作.我可以使用鼠标中键关闭一个选项卡,但它不能在任何其他选项卡上运行(所有选项卡都在运行时添加并继承相同的样式).

所以我需要一些帮助.为什么这只会在第一次工作,而不是之后?显然,我可以创建一个继承自TabItem的自定义控件并使其工作,但我想弄清楚这一点,因为我可以看到它在我的项目中被扩展.我不是DependencyProperties的专家,所以请帮帮我.谢谢!

样式:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="w:Attach.InputBindings">
        <Setter.Value>
            <InputBindingCollection>
                <MouseBinding MouseAction="MiddleClick" 
                              Command="{Binding CloseCommand}"/>
            </InputBindingCollection>
        </Setter.Value>
    </Setter>
    ...
</Style>
Run Code Online (Sandbox Code Playgroud)

public class Attach
{
    public static readonly DependencyProperty InputBindingsProperty =
        DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(Attach),
        new FrameworkPropertyMetadata(new InputBindingCollection(),
        (sender, e) =>
        {
            var element = sender as UIElement;
            if (element == null) return;
            element.InputBindings.Clear();
            element.InputBindings.AddRange((InputBindingCollection)e.NewValue);
        }));

    public static InputBindingCollection GetInputBindings(UIElement element)
    {
        return (InputBindingCollection)element.GetValue(InputBindingsProperty);
    }

    public static void SetInputBindings(UIElement element, InputBindingCollection inputBindings)
    {
        element.SetValue(InputBindingsProperty, inputBindings);
    }
}
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties mvvm inputbinding

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

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万
查看次数

TreeViewItem上的KeyBinding

我有一个典型的树视图和视图模型.viewmodel有一个可观察的其他视图模型集合,用作树的数据源.

public class TreeViewVM {
    public ObservableCollection<ItemVM> Items { get; private set; }
    public ItemVM SelectedItem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和ItemVM:

public class ItemVM {
    public string Name { get; set; }
    public ImageSource Image { get; private set; }
    public ObservableCollection<ItemVM> Children { get; private set; }
    public ICommand Rename { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

风景 :

<TreeView Selecteditem="{Binding SelectedItem}" ItemsSource="{Binding Items}">
    <TreeView.ItemTemplate>
         <HierarchicalDataTemplate>
             <StackPanel Orientation="Horizontal">
                 <StackPanel.InputBindings>
                     <KeyBinding Key="F2" Command="{Binding Rename}"/>
                 </StackPanel.InputBindings>
                 <Image Source="{Binding Image}"/>
                 <TextBlock Text="{Binding …
Run Code Online (Sandbox Code Playgroud)

wpf treeview key-bindings treeviewitem inputbinding

8
推荐指数
2
解决办法
5390
查看次数

如何删除通过CommandManager.RegisterClassInputBinding添加的输入绑定?

我使用CommandManager.RegisterClassInputBinding将绑定添加到整个类型.现在我想删除它.

这是我测试的.

private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
{
    CommandManager.RegisterClassInputBinding(
        typeof(TextBox),
        new InputBinding(TestCommand, new KeyGesture(Key.S, ModifierKeys.Control)));


    MessageBox.Show("CommandBinding_Executed_1");
}
Run Code Online (Sandbox Code Playgroud)

这种方法被称为上Ctrl+ H并注册新的输入结合Ctrl+ S.如果我按Ctrl+ SCtrl+ H这是行不通的,但是当我按下它之后.

我检查过sender.InputBindings,只有一个绑定(Ctrl+ S)所以我得出的结论是RegisterClassInputBinding(),不会将绑定添加到每个现有实例,而是存储与该类关联的绑定,然后将它们与处理过的手势进行比较.

但那么为什么没有RemoveClassInputBinding()方法呢?:(

编辑


我甚至设法通过反射做我想要的,但仍然找不到本机方法,尽管它实现起来很简单.

var fieldInfo = typeof(CommandManager).GetField(
    "_classInputBindings", BindingFlags.Static | BindingFlags.NonPublic);
var fieldData = (HybridDictionary)fieldInfo.GetValue(null);
var inputBindingCollection = (InputBindingCollection)fieldData[typeof(TextBox)];
foreach (var o in inputBindingCollection)
{
    if (o == inputBinding)
    {
        MessageBox.Show("half way there");
    } …
Run Code Online (Sandbox Code Playgroud)

c# wpf inputbinding

7
推荐指数
1
解决办法
2203
查看次数

XAML - 如何拥有全局inputBindings?

我有一个带有几个窗口的WPF应用程序.我想定义GLOBAL inputBindings.

要定义LOCAL inputbindings,我只需在Window.InputBindings或UserControl.InputBindings中声明输入.

要定义GLOBAL,我希望我可以对Application类做同样的事情......

<Application
....>
<Application.InputBindings>
...
</Application.InputBindings>
Run Code Online (Sandbox Code Playgroud)

如果我在2个不同的窗口中有相同的绑定,我必须编码两次.这不符合DRY的理念,我猜有更好的方法......

编辑:在他的回答中,Kent Boogaart建议我使用Style.不幸的是,我无法弄清楚如何定义它.这是代码:

 <Application.Resources>
    <Style TargetType="Window">
        <Setter Property="InputBindings">
            <Setter.Value>
                <Window.InputBindings>
                    <KeyBinding KeyGesture="Ctrl+M" Command="local:App.MsgCommand />
                </Window.InputBindings>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources> 
Run Code Online (Sandbox Code Playgroud)

它引发了一个错误:错误MC3080:无法设置Property Setter'InputBindings',因为它没有可访问的set访问器.

我的风格错了吗?还有其他解决方案吗?

有任何想法吗?谢谢!

wpf xaml inputbinding

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

在Style中定义InputBindings

我想右键点击左键点击命令添加到每个ListBoxItemStyle.这可能吗?

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="InputBindings">
        <Setter.Value>
            <MouseBinding Command="{x:Static View:Commands.AddItem}"
                          MouseAction="LeftClick"/>
            <MouseBinding Command="{x:Static View:Commands.RemoveItem}"
                          MouseAction="RightClick"/>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

c# wpf styles listboxitem inputbinding

6
推荐指数
1
解决办法
3028
查看次数

如果我们无法绑定MouseBinding的命令,我们该怎么办?

我希望能够使用常规MouseBinding来捕获我的CTRL-Click事件TextBlock.不幸的是,该Command属性不是依赖属性,而且我正在使用MVVM,因此我无法将其绑定到我的viewmodel.

微软怎么会遗漏这个基本功能呢?有没有简单的方法来检测CTRL-Click并将它们绑定到我的viewmodel中的命令?

wpf dependency-properties mvvm inputbinding

5
推荐指数
2
解决办法
4444
查看次数

MouseBinding上的WPF键盘修改器

我正在使用WPF中的MVVM模式(两者都有点新).

我想设置InputBinding一个CheckBoxControl + Click事件相对应的on ,但是ModifiersMouseBinding元素上看不到属性.这就是我想要实现的(虚构代码,显然 - 修饰符不存在):

<CheckBox>
     <CheckBox.InputBindings>
           <MouseBinding MouseAction="LeftClick" 
                         Command="{Binding CheckboxControlClickCommand}"
                         Modifiers="Control" />
     </CheckBox.InputBindings>
</CheckBox>
Run Code Online (Sandbox Code Playgroud)

如何在不使用事件的情况下完成此任务的任何想法

谢谢!

wpf checkbox mouseevent inputbinding

4
推荐指数
3
解决办法
2585
查看次数

如何将输入键绑定到wpf中的itemscontrol

我有一个项目控件,它在画布上有项目,当我按下删除我想从画布中删除一个项目:

<ItemsControl.InputBindings>
    <KeyBinding Command="{Binding DeleteItemCommand}" Key="Delete"/>
</ItemsControl.InputBindings>
Run Code Online (Sandbox Code Playgroud)

但是,从不调用DeleteItemCommand中的方法.

我怎样才能做到这一点?

wpf xaml itemscontrol key-bindings inputbinding

2
推荐指数
1
解决办法
1167
查看次数

WPF InputBinding Ctrl + MWheelUp/Down可能吗?

有没有办法可以绑定命令Ctrl+MWheelUp/Down?你知道在浏览器中,你可以做同样的事情来增加/减少字体大小吗?我想在WPF中复制这种效果.可能?我在看InputBinding > MouseBindings,MouseAction似乎不支持Mouse Scrolls.

*我似乎发布了一个类似的问题,但已经找不到了

wpf inputbinding

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