0

I am trying to bind the checked and unchecked of the menuitem to a property by setting the datacontext at the contextmenu. It is not working.thank you

 <xcdg:DataGridControl Name="DataGrid" x:Uid="ExtendedDataGrid" AutomationProperties.AutomationId="ExtendedDataGrid" SelectionChanged="DataGrid_OnSelectionChanged" VirtualizingStackPanel.VirtualizationMode="Recycling" Margin="0,0,0,0" Loaded="DataGrid_OnLoaded" NavigationBehavior="RowOnly" AutoCreateColumns="False" ItemsSource="{Binding ElementName=DataGridContainer, Path=Measurements}"> <ContextMenu x:Key="StatisticsFunctionContextMenu" x:Uid="DataGridStatisticsFunctionContextMenu" AutomationProperties.AutomationId="DataGridStatisticsFunctionContextMenu" DataContext="{Binding ElementName=DataGridContainer, Path=StatsViewModel}"> <MenuItem Name="StatisticsFunctions" Header="{x:Static Properties:Resources.StatisticsFunctionsMenu}" x:Uid="DataGridStatisticsFunctionContextMenu_StatisticsFunctions" AutomationProperties.AutomationId="DataGridStatisticsFunctionContextMenu_StatisticsFunctions"> <MenuItem.Icon> <Image MaxWidth="16" MaxHeight="16" Source="pack://application:,,,/AnalysisView;component/Images/StatisticsFunctions.png"></Image> </MenuItem.Icon> <MenuItem Name = "StatisticsFunctionsShowMenu" IsCheckable="True" IsChecked="{Binding Path=StatisticsFunctionsSetting.IsShowChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Command = "commonview:ApplicationCommands.ShowStatisticsFunctions" Header="{x:Static Properties:Resources.StatisticsFunctionsShowMenu}" /> </MenuItem> </ContextMenu> <!-- STYLE: DataRow --> <Style TargetType="{x:Type xcdg:DataRow}" BasedOn="{StaticResource ZetasizerTableflowViewClassicSystemColorDataRowStyle}"> <Style.Resources> <Style TargetType="xcdg:HierarchicalGroupLevelIndicatorPane"> <Setter Property="Template" Value="{StaticResource ZetasizerTableflowViewHierarchicalGroupLevelIndicatorPaneTemplate}"/> </Style> <Style TargetType="{x:Type xcdg:DataCell}" BasedOn="{StaticResource ZetasizerTableflowViewClassicSystemColorDataCellStyle}"> <Setter Property="ContextMenu" Value="{Binding Source={StaticResource StatisticsFunctionContextMenu} }" /> </Style> </Style.Resources> </Style> 

Error message in the output windows:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=DataGridContainer'. BindingExpression:Path=StatsViewModel; DataItem=null; target element is 'ContextMenu' (Name=''); target property is 'DataContext' (type 'Object')

4
  • 1
    ContextMenu is not part of VisualTree and hence you cannot do any DataBinding Directly to ContextMenu. Refer my answer here stackoverflow.com/questions/30104193/… Commented May 13, 2015 at 14:41
  • Thanks I saw the link. Can you give example of how to use Tag property to overcome this ? Commented May 13, 2015 at 14:50
  • Can you post the XAML and for which control you want to add contextmenu Commented May 13, 2015 at 15:52
  • I have reedited to include the XAML code. Basically the contextmenu is shown when right click on the datarow of a datagrid. the name of the contextmenu is StatisticsFunctionsShowMenu in the xaml. Thank you very much. Commented May 13, 2015 at 22:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.