1

Why can't I bind between two elements which are in a MenuItem within a DataGrid?

This is not binding between multiple MenuItems it relates to binding within items present in the header template of the same MenuItem.

This works fine when the same controls are hosted outside a DataGrid. But in a MenuItem I get binding errors "Cannot find source for binding with reference...". Surely they are in the same visual tree and can reference each other?

Note that this is not a duplicate of ElementName Binding from MenuItem in ContextMenu because the binding scenario is slightly different and none of the answers address this issue.

<DataGrid.ContextMenu>
    <ContextMenu>
        <MenuItem  >
            <MenuItem.Header>
                <StackPanel Orientation="Horizontal">
                    <ComboBox Margin="5 0" Name="comboBox">
                        <ComboBoxItem>1</ComboBoxItem>
                        <ComboBoxItem>2</ComboBoxItem>
                        <ComboBoxItem>3</ComboBoxItem>
                    </ComboBox>
                    <TextBlock Margin="5 0" Text="{Binding ElementName=comboBox, Path=SelectedValue}"></TextBlock>
                </StackPanel>
            </MenuItem.Header>
        </MenuItem>
    </ContextMenu>
</DataGrid.ContextMenu>
Community
  • 1
  • 1
Slugart
  • 4,535
  • 24
  • 32
  • Have you tried setting the datacontext of the context menu as described in the referenced question? – CalebB Apr 02 '15 at 14:48
  • @CalebB doing that will invalidate any other bindings where you need to reference the selected line's DataContext. – Slugart Apr 02 '15 at 15:01

1 Answers1

2

You can use x:Reference to achieve it. Refer below code.

<DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem  >
                    <MenuItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <ComboBox Margin="5 0" x:Name="comboBox">
                                <ComboBoxItem>1</ComboBoxItem>
                                <ComboBoxItem>2</ComboBoxItem>
                                <ComboBoxItem>3</ComboBoxItem>
                            </ComboBox>
                            <TextBlock Margin="5 0" 
                                       Text="{Binding Source={x:Reference comboBox}, 
                                       Path=Text}">
                           </TextBlock>
                        </StackPanel>
                    </MenuItem.Header>
                </MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • 1
    As to the why to quote x:Reference "In WPF and XAML 2006, element references are addressed by the framework-level feature of ElementName binding. For most WPF applications and scenarios, ElementName binding should still be used. Exceptions to this general guidance might include cases where there are data context or other scoping considerations that make data binding impractical and where markup compilation is not involved." – ΩmegaMan Apr 02 '15 at 18:50