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