在样式中定义InputBinding
我正在使用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>
类
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);
}
}
没有找到相关结果
已邀请:
2 个回复
旗低饶彤
谢谢!
细屠截殴些