Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm a little bit lost with bindings. I tried so many things in the last hour, I cannot enumerate all of them. I have an issue with a contextMenu inside a DataTemplate.

To explain: I have a UserControl. Its dataContext is itself. Inside this UserControl, I have an ItemsControl to represent a list of Hyperlink. My ItemsControl itemsSource is bound (it is composed of objects elements). I redefined ItemsControl.ItemTemplate. Inside, I create a HyperLink, with TextBlock as child to make it work, and on this TextBlock, I set a ContextMenu by doing the following.

<TextBlock.ContextMenu>
  <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
    <MenuItem Header="Enregistrer la pièce jointe" Foreground="Black">
      <MenuItem Header="Dans le dossier patient" Command="{Binding DataContext.SaveAttachmentIntPatientFolderCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding FilePath}" Foreground="Black" />
      <MenuItem Header="Enregistrer sous ..." Command="{Binding DataContext.SaveAttachmentAsCommand}" CommandParameter="{Binding FilePath}" Foreground="Black" />
    </MenuItem>
  </ContextMenu>
</TextBlock.ContextMenu>

So I have

UserControl --> ItemsControl --> ItemTemplate --> HyperLink --> TextBlock --> ContextMenu --> ContextMenuItem

I know that my first relative source doesn't work, I have a binding error. What I want is to bind on my UserContorl datacontext, which have these commands.

How can I proceed?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
356 views
Welcome To Ask or Share your Answers For Others

1 Answer

ContextMenu takes the DataContext of the ItemsControl and so it cannot access the ViewModel directly. Also It is not part of the VisualTree and so you cannot do RelativeSource binding. So We need to get the DataContext of the UserControl through TextBlock's Tag property and then bind to ContextMenu. You refer the below code.

<TextBlock Text="{Binding }" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
  <TextBlock.ContextMenu>
    <ContextMenu >
      <MenuItem Header="Enregistrer la pièce jointe" Foreground="Black">
        <MenuItem Header="Dans le dossier patient" 
                  Command="{Binding Path=PlacementTarget.Tag.SaveAttachmentIntPatientFolderCommand,
                            RelativeSource={RelativeSource AncestorType=ContextMenu}}"                                                  
                  Foreground="Black" />
        <MenuItem Header="Enregistrer sous ..." 
                  Command="{Binding Path=PlacementTarget.Tag.SaveAttachmentAsCommand,
                            RelativeSource={RelativeSource AncestorType=ContextMenu}}"  
                  Foreground="Black" />
      </MenuItem>
    </ContextMenu>
  </TextBlock.ContextMenu>
</TextBlock>     

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...