【问题标题】:Defining InputBindings within a Style在样式中定义 InputBindings
【发布时间】:2011-02-09 06:51:24
【问题描述】:

我正在使用 MVVM 设计模式创建一个 WPF 应用程序,并且我正在尝试扩展 TabItem 控件,以便它在用户单击鼠标中键时关闭选项卡。我正在尝试使用 InputBindings 来实现这一点,并且它运行良好,直到我尝试在样式中定义它。我了解到您不能将 InputBindings 添加到样式中,除非您使用 DependencyProperty 附加它。所以我关注了这个类似的帖子here...,它的工作原理......几乎。我可以使用鼠标中键关闭一个选项卡,但它不适用于任何其他选项卡(所有选项卡都是在运行时添加并继承相同的样式)。

所以我需要一些帮助。为什么这只会在第一次工作,而不是之后?显然,我可以创建一个从 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>

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

【问题讨论】:

  • 即使使用 Daniel 建议的代码,我也遇到了上述确切问题。在使用上面的 Attach 类时,似乎有一些很奇怪的东西,尤其是在样式中。我发现在添加 InputBindings 时 DataContext “有时”为空,因此当绑定发生时它无法找到命令。我从未找到解决方案,但我最终复制了绑定,如下面的答案所示。

标签: wpf mvvm dependency-properties inputbinding


【解决方案1】:

您的课程“附加”对我来说效果很好! 如果有人需要,诀窍是使用这样的样式,并带有 x:Shared 修饰符:

<InputBindingCollection x:Key="inputCollection" x:Shared="False">
        <KeyBinding Key="Del" Command="{Binding DeleteItemCommand}"/>
</InputBindingCollection>

<Style TargetType="{x:Type TabItem}">
    <Setter Property="w:Attach.InputBindings" Value="{StaticResource inputCollection}" />
    ...
</Style>

谢谢!

【讨论】:

    【解决方案2】:

    没关系,我自己想出来的。最后我什至没有使用上面的 Attach 类......相反,我在 ControlTemplate 上为 TabItem(这是一个边框)使用了 InputBindings,所以它看起来像这样......我不知道为什么我不认为首先是这个.. :)

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

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多