roc*_*eye 6 wpf xaml inputbinding
我有一个带有几个窗口的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访问器.
我的风格错了吗?还有其他解决方案吗?
有任何想法吗?谢谢!
一种解决方案是使用带有a 的附加属性Style来设置InputBindings应用程序中给定类型的所有控件.不幸的是,既然你不能制作一个"全能" Style(我知道,无论如何),你将不得不为Style你想要设置的每种控件类型创建一个InputBindings(但是,这不应该是太多的控制).下面是一些示例代码,说明如何执行此操作:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public class MyAttached
{
public static readonly DependencyProperty InputBindingsProperty =
DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(MyAttached),
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)
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WpfApplication1"
StartupUri="Window1.xaml">
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="loc:MyAttached.InputBindings">
<Setter.Value>
<InputBindingCollection>
<KeyBinding Key="A" Modifiers="Ctrl" Command="loc:Window1.MyAction" />
</InputBindingCollection>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Button">
<Setter Property="loc:MyAttached.InputBindings">
<Setter.Value>
<InputBindingCollection>
<KeyBinding Key="A" Modifiers="Ctrl" Command="loc:Window1.MyAction" />
</InputBindingCollection>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="loc:Window1.MyAction" Executed="MyAction_Executed" />
</Window.CommandBindings>
<StackPanel>
<Button Content="Try Ctrl+A Here!" />
<TextBox Text="Try Ctrl+A Here!" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class Window1
{
public static readonly RoutedUICommand MyAction = new RoutedUICommand("MyAction", "MyAction", typeof(Window1));
public Window1() { InitializeComponent(); }
private void MyAction_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("MyAction!"); }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10139 次 |
| 最近记录: |