1

Caliburn.Micro を使用した .NET 4.0 アプリケーションがあります。メニュー項目ごとに XAML コードを記述する必要がないように、動的メニューを作成したいと考えています。さらに、各コマンドをキー ジェスチャに関連付けたいと考えています。

私はインターフェースIActionを持っています:

public interface IAction
{
    string Name { get; }
    InputGesture Gesture { get; }
    ICommand Command { get; }      
}

私の ViewModel では、IActions のリストを公開しています。

private List<IAction> _actions;
public List<IAction> Actions
{
    get { return _actions; }
    set
    {
        _actions = value;
        NotifyOfPropertyChange(()=> Actions);
    }
}

次のように、ツールバーをアクションにバインドします。

<ToolBar>
    <Menu ItemsSource="{Binding Actions}">
        <Menu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Header" Value="{Binding Name}" />
                <Setter Property="Command" Value="{Binding Command}" />
            </Style>
        </Menu.ItemContainerStyle>
    </Menu>
</ToolBar>

上記のすべての作品。

私が見逃しているのは、キージェスチャーのデータバインディングです。

どこを読んでも、次のような Window.InputBindings の静的定義を含む例しか見つかりません。

<Window.InputBindings>
  <KeyBinding Key="B" Modifiers="Control" Command="ApplicationCommands.Open" />
</Window.InputBindings>

Window.InputBindings を ItemsControl に単純にカプセル化できれば素晴らしいことですが、うまくいきません。

Window.InputBindings を動的にバインドする方法を知っている人はいますか?

ありがとう!

4

1 に答える 1

2

キー ジェスチャは、ウィンドウ オブジェクトに対して作成する必要があります (ウィンドウ全体に影響を与える場合)。

たとえば、という名前の依存関係プロパティを持つカスタム派生ウィンドウ オブジェクトを作成できると思いますBindableInputBindings。OnChanged コールバックのこのプロパティは、ソース コレクションが変更されるたびにキー バインドを追加/削除します。

編集:いくつかのエラーがあるかもしれません。

public class WindowWithBindableKeys: Window {

    protected static readonly DependencyProperty BindableKeyBindingsProperty = DependencyProperty.Register(
        "BindableKeyBindings", typeof(CollectionOfYourKeyDefinitions), typeof(WindowWithBindableKeys), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnBindableKeyBindingsChanged))
    );

    public CollectionOfYourKeyDefinitions BindableKeyBindings
    {
        get
        {
            return (string)GetValue(BindableKeyBindingsProperty);
        }
        set
        {
            SetValue(BindableKeyBindingsProperty, value);
        }
    }

    private static void OnBindableKeyBindingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as WindowWithBindableKeys).InputBindings.Clear();

        // add the input bidnings according to the BindableKeyBindings
    }

}

次に、XAML で

<mynamespace:WindowWithBindableKeys BindableKeyBindings={Binding YourSourceOfKeyBindings} ... > ...
于 2011-02-16T15:14:35.677 に答える