1
我想要从数据模板中的上下文菜单中绑定到父控件。从数据模板中的上下文菜单中绑定到父控件
不幸的是,我仅限于.NET 3.5,不能使用x:在.NET 4
下面分别介绍引用扩展名是什么,我试图做
<Window x:Class="WpfApplication17.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication17"
Name="window">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Car}">
<Rectangle Width="100" Height="100" Fill="Red">
<Rectangle.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Colour}"/>
<MenuItem Header="{Binding ElementName=window, Path=ActualWidth}"/>
</ContextMenu>
</Rectangle.ContextMenu>
</Rectangle>
</DataTemplate>
</Window.Resources>
</Window>
为例
但我得到“由于上下文菜单不是可视化树的一部分,无法找到与引用绑定的源'ElementName = window'”错误。
编辑:
这很好用! 。然而,它似乎并不当我使用的复合材料收集工作,如下面的
<Window.Resources>
<DataTemplate DataType="{x:Type local:Car}">
<Rectangle Width="100" Height="100" Fill="Red"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<Rectangle.ContextMenu>
<ContextMenu>
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem Header="{Binding Colour}"/>
<MenuItem Header="{Binding Path=PlacementTarget.Tag.ActualWidth, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
<!--<ContextMenu>
<MenuItem Header="{Binding Colour}"/>
<MenuItem Header="{Binding Path=PlacementTarget.Tag.ActualWidth, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>-->
</Rectangle.ContextMenu>
</Rectangle>
</DataTemplate>
</Window.Resources>
这很棒! ..但它似乎并没有工作时,我使用复合集合? - 请看我编辑 – wforl 2013-05-09 23:10:35
对不起,但为此我没有解决方案。同样的问题[这里](http://stackoverflow.com/q/11626114/620360)。也许类似的错误描述[这里](http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a)。 – LPL 2013-05-10 00:05:33