将ContextMenu标记绑定到所有者 [英] Binding ContextMenu Tag to Owner

查看:85
本文介绍了将ContextMenu标记绑定到所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将ContextMenu标签绑定到所有者标签时遇到问题.我这样尝试:

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>

如果使用Snoop(xaml调试器)绑定进行检查,则显示错误:

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

System.Windows.Data.Error:4:找不到与之绑定的源 参考"RealativeSource FindAncestor, AncestorType ='System.Windows.Controls.TextBox',AncestorLevel ='1''. BindingExpression:路径=标签; DataItem = null;目标元素是 'ContextMenu'(Name ='uiContextMenu');目标属性是标记"(类型 '对象')

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')

有人可以帮忙吗?谢谢

推荐答案

我希望这是因为ContextMenuItemControl试图访问未指定的TemplatedParent属性.

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>

参考:如何摆脱烦人的Horizo​​ntalContentAlignment绑定警告?

希望有帮助.

这篇关于将ContextMenu标记绑定到所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆