How to bind MenuItem.Header to Window/UserControl dependency property?

后端 未结 2 498
礼貌的吻别
礼貌的吻别 2021-01-06 09:30

I`m wondering how can I bind MenuItem.Header to the parent Window/UserControl dependency property? Here is a simple example:

Window1.xaml:



        
相关标签:
2条回答
  • 2021-01-06 10:07

    Alternative to H.B.'s solution is this attached behavior: ContextMenuServiceExtensions.DataContext Attached Property

    0 讨论(0)
  • 2021-01-06 10:29

    You should see this in the Output window of Visual Studio:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=self'. BindingExpression:Path=MenuText; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Header' (type 'Object')

    That is because the ContextMenu is disconnected from the VisualTree, you need to do this Binding differently.

    One way is via ContextMenu.PlacementTarget (which should be the Grid), you could use its DataContext to establish a binding, e.g.:

    <MenuItem Header="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.MenuText}"/>
    

    or set up the DataContext in the ContextMenu itself:

    <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.DataContext}">
        <MenuItem Header="{Binding Path=MenuText}"/>
    </ContextMenu>
    

    If this is not an option (because the DataContext of the Grid cannot be the Window/UserControl) you can try to pass the reference to the Window/UserControl through the Tag of your Grid for example.

    <Grid ...
          Tag="{x:Reference self}">
        <Grid.ContextMenu>
            <!-- The DataContext is now bound to PlacementTarget.Tag -->
            <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.Tag}">
                <MenuItem Header="{Binding Path=MenuText}"/>
            </ContextMenu>
        ...
    

    As a side-note: Because of this behavior i tend to define a helper-style in App.xaml to make all ContextMenus "pseudo-inherit" the DataContext from their parent:

        <!-- Context Menu Helper -->
        <Style TargetType="{x:Type ContextMenu}">
            <Setter Property="DataContext" Value="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"/>
        </Style>
    
    0 讨论(0)
提交回复
热议问题