Use Window DataContext in child element after overriding DataContext in parent










0















I've got a viewmodel that contains a collection of objects and a bunch of commands.



public class MainWindowVM : NotifyPropertyChangedBase

private CollectionViewSource employeeViewSource;

private ICommand cmdOpenDetailEmployee;

public MainWindowVM()

nsDataProviderEmployees = new NSDataProvider();

employeeViewSource = new CollectionViewSource();

cmdOpenDetailEmployee = new RelayCommand<object>((parameter) => ...);

this.LoadData();


public CollectionViewSource EmployeeViewSource => employeeViewSource;
public ICommand CmdOpenDetailEmployee => cmdOpenDetailEmployee;



In my application I want to use this command in a context menu of the datagrid showing the employees.



<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.UI"
xmlns:DataModel="clr-namespace:MyApp.DataModel;assembly=MyApp.DataModel" x:Class="MyApp.UI.MainWindow"
xmlns:vm="clr-namespace:MyApp.UI.ViewModels"
mc:Ignorable="d"
Title="MyApp - Main" Height="751.826" Width="1111.005" Loaded="Window_Loaded" Icon="Resources/MyApp.ico">
<Window.DataContext>
<vm:MainWindowVM />
</Window.DataContext>
<Grid x:Name="grdMain">
<DataGrid DataContext="Binding Path=EmployeeViewSource" x:Name="employeeDataGrid" EnableRowVirtualization="True" ItemsSource="Binding" Margin="10,77,10,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu DataContext="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type Window">
<MenuItem Header="OpenDetail..."
Command="Binding CmdOpenDetailEmployee"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>...</DataGrid.Columns>

</DataGrid>
</Grid>
</Window>


The Problem is I am unable to come up with a combination of bindings that both let me use the EmployeeViewSource property or my ViewModel as DataContext for the grid AND the CmdOpenDetailEmployee property of my ViewModel as the DataContext for my ContextMenu and MenuItems.



According to all the posts I've been able to find this should work but the command isn't executed when I click the menu item.










share|improve this question






















  • First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

    – elgonzo
    Nov 13 '18 at 11:04












  • I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

    – elgonzo
    Nov 13 '18 at 11:09












  • @elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

    – Kempeth
    Nov 13 '18 at 12:26















0















I've got a viewmodel that contains a collection of objects and a bunch of commands.



public class MainWindowVM : NotifyPropertyChangedBase

private CollectionViewSource employeeViewSource;

private ICommand cmdOpenDetailEmployee;

public MainWindowVM()

nsDataProviderEmployees = new NSDataProvider();

employeeViewSource = new CollectionViewSource();

cmdOpenDetailEmployee = new RelayCommand<object>((parameter) => ...);

this.LoadData();


public CollectionViewSource EmployeeViewSource => employeeViewSource;
public ICommand CmdOpenDetailEmployee => cmdOpenDetailEmployee;



In my application I want to use this command in a context menu of the datagrid showing the employees.



<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.UI"
xmlns:DataModel="clr-namespace:MyApp.DataModel;assembly=MyApp.DataModel" x:Class="MyApp.UI.MainWindow"
xmlns:vm="clr-namespace:MyApp.UI.ViewModels"
mc:Ignorable="d"
Title="MyApp - Main" Height="751.826" Width="1111.005" Loaded="Window_Loaded" Icon="Resources/MyApp.ico">
<Window.DataContext>
<vm:MainWindowVM />
</Window.DataContext>
<Grid x:Name="grdMain">
<DataGrid DataContext="Binding Path=EmployeeViewSource" x:Name="employeeDataGrid" EnableRowVirtualization="True" ItemsSource="Binding" Margin="10,77,10,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu DataContext="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type Window">
<MenuItem Header="OpenDetail..."
Command="Binding CmdOpenDetailEmployee"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>...</DataGrid.Columns>

</DataGrid>
</Grid>
</Window>


The Problem is I am unable to come up with a combination of bindings that both let me use the EmployeeViewSource property or my ViewModel as DataContext for the grid AND the CmdOpenDetailEmployee property of my ViewModel as the DataContext for my ContextMenu and MenuItems.



According to all the posts I've been able to find this should work but the command isn't executed when I click the menu item.










share|improve this question






















  • First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

    – elgonzo
    Nov 13 '18 at 11:04












  • I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

    – elgonzo
    Nov 13 '18 at 11:09












  • @elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

    – Kempeth
    Nov 13 '18 at 12:26













0












0








0








I've got a viewmodel that contains a collection of objects and a bunch of commands.



public class MainWindowVM : NotifyPropertyChangedBase

private CollectionViewSource employeeViewSource;

private ICommand cmdOpenDetailEmployee;

public MainWindowVM()

nsDataProviderEmployees = new NSDataProvider();

employeeViewSource = new CollectionViewSource();

cmdOpenDetailEmployee = new RelayCommand<object>((parameter) => ...);

this.LoadData();


public CollectionViewSource EmployeeViewSource => employeeViewSource;
public ICommand CmdOpenDetailEmployee => cmdOpenDetailEmployee;



In my application I want to use this command in a context menu of the datagrid showing the employees.



<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.UI"
xmlns:DataModel="clr-namespace:MyApp.DataModel;assembly=MyApp.DataModel" x:Class="MyApp.UI.MainWindow"
xmlns:vm="clr-namespace:MyApp.UI.ViewModels"
mc:Ignorable="d"
Title="MyApp - Main" Height="751.826" Width="1111.005" Loaded="Window_Loaded" Icon="Resources/MyApp.ico">
<Window.DataContext>
<vm:MainWindowVM />
</Window.DataContext>
<Grid x:Name="grdMain">
<DataGrid DataContext="Binding Path=EmployeeViewSource" x:Name="employeeDataGrid" EnableRowVirtualization="True" ItemsSource="Binding" Margin="10,77,10,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu DataContext="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type Window">
<MenuItem Header="OpenDetail..."
Command="Binding CmdOpenDetailEmployee"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>...</DataGrid.Columns>

</DataGrid>
</Grid>
</Window>


The Problem is I am unable to come up with a combination of bindings that both let me use the EmployeeViewSource property or my ViewModel as DataContext for the grid AND the CmdOpenDetailEmployee property of my ViewModel as the DataContext for my ContextMenu and MenuItems.



According to all the posts I've been able to find this should work but the command isn't executed when I click the menu item.










share|improve this question














I've got a viewmodel that contains a collection of objects and a bunch of commands.



public class MainWindowVM : NotifyPropertyChangedBase

private CollectionViewSource employeeViewSource;

private ICommand cmdOpenDetailEmployee;

public MainWindowVM()

nsDataProviderEmployees = new NSDataProvider();

employeeViewSource = new CollectionViewSource();

cmdOpenDetailEmployee = new RelayCommand<object>((parameter) => ...);

this.LoadData();


public CollectionViewSource EmployeeViewSource => employeeViewSource;
public ICommand CmdOpenDetailEmployee => cmdOpenDetailEmployee;



In my application I want to use this command in a context menu of the datagrid showing the employees.



<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp.UI"
xmlns:DataModel="clr-namespace:MyApp.DataModel;assembly=MyApp.DataModel" x:Class="MyApp.UI.MainWindow"
xmlns:vm="clr-namespace:MyApp.UI.ViewModels"
mc:Ignorable="d"
Title="MyApp - Main" Height="751.826" Width="1111.005" Loaded="Window_Loaded" Icon="Resources/MyApp.ico">
<Window.DataContext>
<vm:MainWindowVM />
</Window.DataContext>
<Grid x:Name="grdMain">
<DataGrid DataContext="Binding Path=EmployeeViewSource" x:Name="employeeDataGrid" EnableRowVirtualization="True" ItemsSource="Binding" Margin="10,77,10,0" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu DataContext="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=x:Type Window">
<MenuItem Header="OpenDetail..."
Command="Binding CmdOpenDetailEmployee"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Columns>...</DataGrid.Columns>

</DataGrid>
</Grid>
</Window>


The Problem is I am unable to come up with a combination of bindings that both let me use the EmployeeViewSource property or my ViewModel as DataContext for the grid AND the CmdOpenDetailEmployee property of my ViewModel as the DataContext for my ContextMenu and MenuItems.



According to all the posts I've been able to find this should work but the command isn't executed when I click the menu item.







c# wpf data-binding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 10:55









KempethKempeth

1,28921529




1,28921529












  • First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

    – elgonzo
    Nov 13 '18 at 11:04












  • I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

    – elgonzo
    Nov 13 '18 at 11:09












  • @elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

    – Kempeth
    Nov 13 '18 at 12:26

















  • First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

    – elgonzo
    Nov 13 '18 at 11:04












  • I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

    – elgonzo
    Nov 13 '18 at 11:09












  • @elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

    – Kempeth
    Nov 13 '18 at 12:26
















First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

– elgonzo
Nov 13 '18 at 11:04






First, see whether the problem is actually with the binding or with the command. Check if you can find binding errors (see also here: stackoverflow.com/questions/8850143/…). Also check in the debugger if the Execute method of your command is actually being invoked. From the question it is not really possible to tell more...

– elgonzo
Nov 13 '18 at 11:04














I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

– elgonzo
Nov 13 '18 at 11:09






I retract the last sentence of my previous comment. :-) Come to think of it, it might be the binding using FindAncestor. Since the context menu is a popup window, it might not have the main window as an ancestor (i am not 100% sure here). The binding could very well just find the popup window element of the context menu (if there is such). Try using a binding with ElementName instead (you would need to give your main window a name, of course), and see what happens...

– elgonzo
Nov 13 '18 at 11:09














@elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

– Kempeth
Nov 13 '18 at 12:26





@elgonzo Here's there error from the Output System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:(no path); DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') The execute method of my command isn't called. And I haven't managed to get a binding with ElementName to work either: Command="Binding ElementName=mainWindow, Path=CmdOpenDetailEmployee"

– Kempeth
Nov 13 '18 at 12:26












1 Answer
1






active

oldest

votes


















0














Bind to the DataContext of the PlacementTarget of the ContextMenu, and let the DataGrid inherit its DataContext from the window:



<DataGrid x:Name="employeeDataGrid" 
EnableRowVirtualization="True"
ItemsSource="Binding EmployeeViewSource.View" Margin="10,77,10,0"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="OpenDetail..."
Command="Binding PlacementTarget.DataContext.CmdOpenDetailEmployee,
RelativeSource=RelativeSource AncestorType=ContextMenu"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<!--<DataGrid.Columns>...</DataGrid.Columns>-->
</DataGrid>





share|improve this answer

























  • If I remove the DataContext from the DataGrid then I no longer get any data displayed...

    – Kempeth
    Nov 13 '18 at 12:15











  • Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

    – mm8
    Nov 13 '18 at 12:19












  • Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

    – Kempeth
    Nov 13 '18 at 12:31










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279458%2fuse-window-datacontext-in-child-element-after-overriding-datacontext-in-parent%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Bind to the DataContext of the PlacementTarget of the ContextMenu, and let the DataGrid inherit its DataContext from the window:



<DataGrid x:Name="employeeDataGrid" 
EnableRowVirtualization="True"
ItemsSource="Binding EmployeeViewSource.View" Margin="10,77,10,0"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="OpenDetail..."
Command="Binding PlacementTarget.DataContext.CmdOpenDetailEmployee,
RelativeSource=RelativeSource AncestorType=ContextMenu"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<!--<DataGrid.Columns>...</DataGrid.Columns>-->
</DataGrid>





share|improve this answer

























  • If I remove the DataContext from the DataGrid then I no longer get any data displayed...

    – Kempeth
    Nov 13 '18 at 12:15











  • Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

    – mm8
    Nov 13 '18 at 12:19












  • Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

    – Kempeth
    Nov 13 '18 at 12:31















0














Bind to the DataContext of the PlacementTarget of the ContextMenu, and let the DataGrid inherit its DataContext from the window:



<DataGrid x:Name="employeeDataGrid" 
EnableRowVirtualization="True"
ItemsSource="Binding EmployeeViewSource.View" Margin="10,77,10,0"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="OpenDetail..."
Command="Binding PlacementTarget.DataContext.CmdOpenDetailEmployee,
RelativeSource=RelativeSource AncestorType=ContextMenu"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<!--<DataGrid.Columns>...</DataGrid.Columns>-->
</DataGrid>





share|improve this answer

























  • If I remove the DataContext from the DataGrid then I no longer get any data displayed...

    – Kempeth
    Nov 13 '18 at 12:15











  • Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

    – mm8
    Nov 13 '18 at 12:19












  • Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

    – Kempeth
    Nov 13 '18 at 12:31













0












0








0







Bind to the DataContext of the PlacementTarget of the ContextMenu, and let the DataGrid inherit its DataContext from the window:



<DataGrid x:Name="employeeDataGrid" 
EnableRowVirtualization="True"
ItemsSource="Binding EmployeeViewSource.View" Margin="10,77,10,0"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="OpenDetail..."
Command="Binding PlacementTarget.DataContext.CmdOpenDetailEmployee,
RelativeSource=RelativeSource AncestorType=ContextMenu"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<!--<DataGrid.Columns>...</DataGrid.Columns>-->
</DataGrid>





share|improve this answer















Bind to the DataContext of the PlacementTarget of the ContextMenu, and let the DataGrid inherit its DataContext from the window:



<DataGrid x:Name="employeeDataGrid" 
EnableRowVirtualization="True"
ItemsSource="Binding EmployeeViewSource.View" Margin="10,77,10,0"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="OpenDetail..."
Command="Binding PlacementTarget.DataContext.CmdOpenDetailEmployee,
RelativeSource=RelativeSource AncestorType=ContextMenu"
CommandParameter="Binding"/>
</ContextMenu>
</DataGrid.ContextMenu>
<!--<DataGrid.Columns>...</DataGrid.Columns>-->
</DataGrid>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 12:23

























answered Nov 13 '18 at 12:04









mm8mm8

83.3k81931




83.3k81931












  • If I remove the DataContext from the DataGrid then I no longer get any data displayed...

    – Kempeth
    Nov 13 '18 at 12:15











  • Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

    – mm8
    Nov 13 '18 at 12:19












  • Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

    – Kempeth
    Nov 13 '18 at 12:31

















  • If I remove the DataContext from the DataGrid then I no longer get any data displayed...

    – Kempeth
    Nov 13 '18 at 12:15











  • Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

    – mm8
    Nov 13 '18 at 12:19












  • Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

    – Kempeth
    Nov 13 '18 at 12:31
















If I remove the DataContext from the DataGrid then I no longer get any data displayed...

– Kempeth
Nov 13 '18 at 12:15





If I remove the DataContext from the DataGrid then I no longer get any data displayed...

– Kempeth
Nov 13 '18 at 12:15













Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

– mm8
Nov 13 '18 at 12:19






Why are you defining a CollectionViewSource in the view model? Anyway, you could bind to EmployeeViewSource.View. See my edit.

– mm8
Nov 13 '18 at 12:19














Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

– Kempeth
Nov 13 '18 at 12:31





Legacy code which I'm trying to move towards MVVM. I'm pretty new to this myself. What would be a better approach? --- Either way: If I remove the DataGrid DataContext and bind ItemsSource to the View the grid works and I can bind the MenuItem straight to the command with Binding CmdOpenDetailEmployee Thanks!

– Kempeth
Nov 13 '18 at 12:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279458%2fuse-window-datacontext-in-child-element-after-overriding-datacontext-in-parent%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo