在样式中定义InputBindings [英] Defining InputBindings within a Style

查看:395
本文介绍了在样式中定义InputBindings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM设计模式创建WPF应用,并且尝试扩展TabItem控件,以便当用户单击鼠标中键时它关闭选项卡.我正在尝试使用InputBindings来实现这一点,并且在我尝试在样式中定义它之前,它工作得很好.我了解到,除非使用DependencyProperty附加输入绑定,否则无法将其添加到样式.因此,我在此处... 上关注了这篇类似的文章,它确实有效...几乎.我可以使用鼠标中键关闭一个选项卡,但在其他任何选项卡上均不起作用(所有选项卡都在运行时添加,并且继承了相同的样式).

I'm creating a WPF app using the MVVM design pattern, and I'm trying to extend the TabItem control so that it closes the tab when the user clicks the middle mouse button. I'm trying to achieve this using InputBindings, and it works very well until I try to define it within a style. I've learned that you cannot add InputBindings to a style unless you attach it using a DependencyProperty. So I followed this similar post here... and it works... almost. I can close one tab using the middle mouse button, but it won't work on any of the other tabs (all of the tabs are added at runtime and inherit the same style).

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

So I need some help. Why would this only be working the first time, and not after? Obviously I could create a custom control that inherits from a TabItem and make it work, but I'd like to figure this out as I can see this being expanded in my projects. I'm no expert on DependencyProperties, so please help me out. Thanks!

样式:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="w:Attach.InputBindings">
        <Setter.Value>
            <InputBindingCollection>
                <MouseBinding MouseAction="MiddleClick" 
                              Command="{Binding CloseCommand}"/>
            </InputBindingCollection>
        </Setter.Value>
    </Setter>
    ...
</Style>

班级

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);
    }
}

推荐答案

没关系,我自己弄清楚了.我最终甚至没有使用上面的Attach类...而是在ControlTemplate上为TabItem(这是一个Border)使用了InputBindings,所以看起来像这样……我不知道为什么我没有想到首先是这个..:)

Never mind, I figured it out myself. I ended up not even using the Attach class above... instead I used InputBindings on the ControlTemplate for the TabItem (which is a Border), so it looked something like this... I don't know why I didn't think of this in the first place.. :)

<ControlTemplate TargetType="{x:Type TabItem}">
    <Grid SnapsToDevicePixels="true">
        <Border x:Name="Bd" ...>
            <DockPanel>
                ...
            </DockPanel>
            <Border.InputBindings>
                <MouseBinding MouseAction="MiddleClick"
                              Command="{Binding CloseCommand}"/>
            </Border.InputBindings>
        </Border>
    </Grid>
    ...
</ControlTemplate>

这篇关于在样式中定义InputBindings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆