Bind Items to MenuItem -> use Command

余生颓废 提交于 2019-12-13 12:09:43

问题


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
        • File 1
        • File 2
        • File 3

XAML Code:

<Menu>
<MenuItem Header="File">
    <MenuItem Header="Open">
        <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}"/>
    </MenuItem>
</MenuItem>
</Menu>

I want to call a Command, when a specific item has been clicked. Example: User clicks on File 1, a command should be called where the "File 1" is the Command Parameter.

ViewModel contains the Items, which I want to display in the MenuItem "collection"

private ObservableCollection<string> _OCFragebogen;
public ObservableCollection<string> OCFragebogen
{
    get
    {
        if (_OCFragebogen == null)
            _OCFragebogen = new ObservableCollection<string>();
        return _OCFragebogen;
    }
    set
    {
        _OCFragebogen = value;
        RaisePropertyChanged(() => OCFragebogen);
    }
}

To make it clear: When the user clicks on an item (from the ItemsSource) in the MenuItem, a Command should be called where I want to do something with the clicked Item.

Edit: Where do I have to use the command to call a method (RelayCommand) in my ViewModel? I want it to be used when an Item from the ItemsSource has been clicked + I want to pass the clicked item to the method.


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/17830587/bind-items-to-menuitem-use-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!