Bind Items to MenuItem -> use Command

后端 未结 2 832
独厮守ぢ
独厮守ぢ 2020-12-18 12:16

I have a MenuItem, which has a collection of items in it. It looks like the File -> Open Menuitem.

So:

  • File
    • Open
      • Open from DataBase <
相关标签:
2条回答
  • 2020-12-18 12:37

    Try to change its ItemContainerStyle and then bind the command from ItemsSource item,

    <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}"
    <MenuItem.ItemContainerStyle>
      <Style TargetType="MenuItem">
        <Setter Property="Command" Value="{Binding YourCommand}" />
      </Style>
    </MenuItem.ItemContainerStyle>
    </MenuItem>
    

    I haven't tried if it works just a guess

    edited answer

                    <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}">
                        <MenuItem.ItemContainerStyle>
                            <Style TargetType="MenuItem">
                                <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}, Path=DataContext.YourCommand }" />
                                <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Self},  Path=Header}"/>
                            </Style>
                        </MenuItem.ItemContainerStyle>
                    </MenuItem>
    
    0 讨论(0)
  • 2020-12-18 12:44

    This should work for you

    <MenuItem Header="From Database" 
              ItemsSource="{Binding YourItemSource}">
        <MenuItem.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.YourCommandName}"></Setter>
                <Setter Property="CommandParameter" Value="{Binding}"></Setter>
             </Style>
         </MenuItem.ItemContainerStyle>
    </MenuItem>
    
    0 讨论(0)
提交回复
热议问题