0

I have the following XAML that loads the menus and menuitems, but is there a way to bind a command/inputgesture without having to use a StackPanel? CmdParam is the property in my VM that has the command. I initially thought of setting it in the resource section like below, but the command doesnt show up

<Window>  
    <Window.Resources>        
        <Style TargetType="{x:Type MenuItem}" x:Key="menuSeparatorStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Separator HorizontalAlignment="Stretch" IsEnabled="false"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Setter Property="InputGestureText" Value="{Binding CmdParam}"/>
        </Style>      

    <HierarchicalDataTemplate DataType="{x:Type projectBusinessLayer:MenuDataItem}">

        <ContentPresenter Content="{Binding Path=MenuText}" Loaded="OnMenuDataItemLoaded" RecognizesAccessKey="True" />

        <HierarchicalDataTemplate.ItemsSource>
            <Binding Path="MenuItemID">
                <Binding.Converter>
                    <converters:MenuDataItemConverter />
                </Binding.Converter>
            </Binding>
        </HierarchicalDataTemplate.ItemsSource>
    </HierarchicalDataTemplate>

    <projectBusinessLayer:MenuSeparatorStyleSelector x:Key="menuSeparatorStyleSelector"/>
</Window.Resources>

<DockPanel>
    <Menu x:Name="mnuMainMenu" ItemsSource="{Binding}" VerticalAlignment="Top" DockPanel.Dock="Top" ItemContainerStyleSelector="{StaticResource menuSeparatorStyleSelector}"/>
</DockPanel>

1 Answer 1

1

I don't fully understand what you mean with dynamically, but what about...

<Window.InputBindings>
    <KeyBinding Key="F7" Command="{Binding CmdParam}" />
</Window.InputBindings>

ViewModel:

private ICommand _cmdParam;
public ICommand CmdParam
{
      get
      {
          if (_cmdParam== null)
             _cmdParam= new DelegateCommand(DoSomething);
           return _cmdParam;
      }
}

private void DoSomething(object obj)
{
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.