1
votes

I have a problem with binding ContextMenu Tag to Owner Tag. I trying like this:

 <Style x:Key="DefaultTextBox" TargetType="{x:Type TextBox}">
                <Setter Property="BorderBrush" Value="{DynamicResource ThemeSecondary}"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu x:Name="uiContexMenu">
                            <ContextMenu.ItemsSource>
                                <CompositeCollection>
                                    <MenuItem Command="Cut" Header="Cut">
                                        <MenuItem.Icon>
                                            <Viewbox Width="16" Height="16">
                                                <TextBlock FontFamily="{DynamicResource IconFont}" Text=""/>
                                            </Viewbox>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Command="Copy" Header="Copy">
                                        <MenuItem.Icon>
                                            <Viewbox Width="16" Height="16">
                                                <TextBlock FontFamily="{DynamicResource IconFont}" Text=""/>
                                            </Viewbox>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Command="Paste" Header="Paste">
                                        <MenuItem.Icon>
                                            <Viewbox Width="16" Height="16">
                                                <TextBlock FontFamily="{DynamicResource IconFont}" Text=""/>
                                            </Viewbox>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <CollectionContainer Collection="{Binding ElementName=uiContexMenu, Path=Tag(local:Extensions.ExtendCommands)}"/>
                                </CompositeCollection>
                            </ContextMenu.ItemsSource>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>

If check with Snoop (xaml debuger) binding show error:

System.Windows.Data.Error: 4: Cannot find source for binding with reference 'RealativeSource FindAncestor, AncestorType='System.Windows.Controls.TextBox', AncestorLevel='1''. BindingExpression: Path=Tag; DataItem=null; target element is 'ContextMenu'(Name='uiContextMenu'); target property is 'Tag' (type 'Object')

Can someone help? Thanks

1
Never saw this Path=Tag(local:Extensions.ExtendCommands) .AnjumSKhan

1 Answers

0
votes

I hope this is because the ItemControl of ContextMenu trying to access the property of TemplatedParent which haven't specified.

Anyway, this will fix your issue.

<Application.Resources>
    <Style TargetType="MenuItem">
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    </Style>
</Application.Resources>

Reference: How to get rid of annoying HorizontalContentAlignment binding warning?

Hope that helps.