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

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

问题描述

我正在使用 MVVM 设计模式创建 WPF 应用程序,并且我正在尝试扩展 TabItem 控件,以便它在用户单击鼠标中键时关闭选项卡.我正在尝试使用 InputBindings 来实现这一点,并且在我尝试在样式中定义它之前它工作得很好.我了解到您不能将 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 类......相反,我在 TabItem(这是一个边框)的 ControlTemplate 上使用了 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天全站免登陆