7

複数のウィンドウを持つ WPF アプリケーションがあります。GLOBAL inputBindings を定義したいと思います。

LOCAL 入力バインディングを定義するには、Window.InputBindings または UserControl.InputBindings で入力を宣言するだけです。

GLOBAL を定義するには、Application クラスでも同じことができたらいいのにと思います...

<Application
....>
<Application.InputBindings>
...
</Application.InputBindings>

2 つの異なるウィンドウで同じバインディングを使用している場合は、2 回コーディングする必要があります。これはDRYの哲学を満たしていません。もっと良い方法があると思います...

EDIT : 彼の回答で、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> 

エラーが発生します: エラー MC3080: アクセス可能な set アクセサーがないため、プロパティ セッター 'InputBindings' を設定できません。

私のスタイルは間違っていますか?別の解決策はありますか?

何か案は?ありがとう!

4

2 に答える 2

10

1つの解決策は、添付プロパティをで使用して、アプリケーション内の特定のタイプのすべてのコントロールに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);
        }

    }
}

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

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

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!"); }
    }
}
于 2009-09-03T22:52:27.263 に答える
0

Styleすべての に適用される を作成できますWindow。を設定StyleできInputBindingsます。

于 2009-07-09T16:05:46.157 に答える