0

我有以下XAML加载菜单和菜单项的内容,但是有没有办法绑定命令/输入手势而不必使用StackPanelCmdParam是我的 VM 中具有该命令的属性。我最初想在下面的资源部分中设置它,但命令没有显示

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

4

1 回答 1

1

我不完全理解你动态的意思,但是......

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

视图模型:

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

private void DoSomething(object obj)
{
}
于 2013-06-10T13:13:31.950 回答