AndersLindas
AndersLindas

Reputation: 137

Access my Window's DataContext inside a DataTemplate

I'm working on a WPF application and I'm using the MVVM pattern. I use MVVMLight to help me handle some Events. I need to forward the "Click" event so that I can pass the arguments as well so that I can know for sure which item that sent the event. If I use the "Command" I cant know for sure that it was the selected item that sent the event - as the item doesnt need to be selected to right click on it.

This is my code for displaying a list of "order lines". There are two types of order lines, and for one of the data types; "AccessoryOrderLine" - I want to add a context menu.

My problem is that I cannot access my Window's DataContext. I've named the root node in the Window "root", and I'm trying to access the root's DataContext, but this failes with the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=root'. BindingExpression:Path=DataContext.PackAccessory; DataItem=null; target element is 'EventToCommand' (HashCode=5903270); target property is 'Command' (type 'ICommand')

<ListBox HorizontalContentAlignment="Stretch" Margin="10,0,10,10" DockPanel.Dock="Bottom" Grid.Row="1" ItemsSource="{Binding OrderLines, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type m:UnitOrderLine}">
             <v:OrderLine />
        </DataTemplate>
        <DataTemplate DataType="{x:Type m:AccessoryOrderLine}">
            <v:OrderLine>
                <v:OrderLine.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Pack 1" IsCheckable="False">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <cmd:EventToCommand Command="{Binding ElementName=root, Path=DataContext.PackAccessory }" PassEventArgsToCommand="True" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                         </MenuItem>
                     </ContextMenu>
                 </v:OrderLine.ContextMenu>
             </v:OrderLine>
         </DataTemplate>
     </ListBox.Resources>
 </ListBox>

I've also tried to use "TemplatedParent" and then I get access to my "OrderLine" DataContext, but I cant get one step further back to my "MainWindowModel".

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

Found a solution to my problem :)

Upvotes: 1

Views: 2408

Answers (3)

AndersLindas
AndersLindas

Reputation: 137

Found a solution. Updated my original post with the link to my solution.

Upvotes: 1

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

Seems like here are answers for your question:

Upvotes: 0

Rohit Vats
Rohit Vats

Reputation: 81313

Its not issue with the DataTemplate. Binding with ElemetName works in all cases except in the case of ContextMenu since it does not lies in the same visual tree as of your window. However, there is one hack where you can use the PlacementTarget property of your context menu.

For details refer to this link - http://social.msdn.microsoft.com/Forums/nl/wpf/thread/526ab350-8788-4bc6-a98a-1e4dee6ad33a It contains exactly what you are trying to achieve here.

Upvotes: 0

Related Questions