将ContextMenu标记绑定到所有者 [英] Binding ContextMenu Tag to Owner
问题描述
我在将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')
有人可以帮忙吗?谢谢
推荐答案
我希望这是因为ContextMenu
的ItemControl
试图访问未指定的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>
参考:如何摆脱烦人的HorizontalContentAlignment绑定警告?
希望有帮助.
这篇关于将ContextMenu标记绑定到所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!