2

I have a problem with the RibbonMenuButton. Currently I have :

<RibbonMenuButton Label="Meeting" Width="Auto" ToolTipDescription="Display requests on the agenda for the meeting selected" ToolTipTitle="Meeting" 
                                  LargeImageSource="pack://application:,,,/Resources/meeting.png"
                                  ItemsSource="{Binding MeetingsAvailable}">                       

    <RibbonMenuButton.ItemTemplate>                            
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </RibbonMenuButton.ItemTemplate>                                         
</RibbonMenuButton>

My MeetingsAvailable is actually a Dictionary<int, string>. This code is working, the RibbonMenuButtonis well displaying each Value of the dictionnary.

Now I'm trying to get back the Key of the MenuItem which has been clicked. My idea was to use a ICommand in my ViewModel and to bind an event to this command. But I don't really know how to get the event corresponding to clicking an Item in the RibbonMenuButton

Do someone have already did that ?

Thank you in advance.

Bastien
  • 994
  • 11
  • 25
  • *I have no idea how to deal to bind the "I clicked on an item" to a specific command in my ViewModel ( I want to get back the Key)*... this is *not* understandable English. Please edit your question to clarify exactly what you are trying to do and what problem(s) you are having. – Sheridan Jun 13 '14 at 10:12
  • Yes, that's more understandable... thank you. I have now removed my close vote. – Sheridan Jun 13 '14 at 11:04
  • Thanks, and sorry for the first version of the question – Bastien Jun 13 '14 at 11:06
  • There's really no need to apologise... it's just that if we can't understand your question, then we can't answer it. – Sheridan Jun 13 '14 at 11:09

2 Answers2

3

You can data bind an ICommand to a RibbonMenuButton using the ItemContainerStyle property, like this:

<RibbonMenuButton Label="Meeting" ItemsSource="{Binding MeetingsAvailable}" ... >
    <RibbonMenuButton.ItemTemplate>                            
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </RibbonMenuButton.ItemTemplate>
    <RibbonMenuButton.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding DataContext.NameOfCommand,
                RelativeSource={RelativeSource AncestorType={x:Type Views:View}}}" />
            <Setter Property="CommandParameter" Value="{Binding Key}" />
        </Style>
    </RibbonMenuButton.ItemContainerStyle>                                         
</RibbonMenuButton>
Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

You have to create a command in your VM to which you can bind. Then you have to do a binding of the key of your dictionary to the command parameter so you can use it inside your commandfunction. Maybe you have to create an additional Button inside your DataTemplate.

Sascha
  • 65
  • 7