Binding ContextMenu Tag to Owner

2019-08-06 06:22发布

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条回答
该账号已被封号
2楼-- · 2019-08-06 06:50

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.

查看更多
登录 后发表回答