【问题标题】:Bind to parent control from within context menu within data template从数据模板的上下文菜单中绑定到父控件
【发布时间】:2013-05-04 10:47:58
【问题描述】:

我想从数据模板中的上下文菜单绑定到父控件。

不幸的是,我仅限于 .net 3.5,无法使用 .net 4 中引入的 x:reference 扩展。

以下是我正在尝试做的示例

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

【问题讨论】:

    标签: wpf binding contextmenu datatemplate elementname


    【解决方案1】:

    请试试这个:

    <DataTemplate DataType="{x:Type local:Car}">
        <Rectangle Width="100" Height="100" Fill="Red"
                   Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
            <Rectangle.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="{Binding Colour}"/>
                    <MenuItem Header="{Binding Path=PlacementTarget.Tag.ActualWidth, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                </ContextMenu>
            </Rectangle.ContextMenu>
        </Rectangle>
    </DataTemplate>
    

    也请查看我的回答here。

    【讨论】:

    • 效果很好! ..但是当我使用复合集合时它似乎不起作用? - 请看我的编辑
    • 抱歉,对此我没有办法。同样的问题here。可能与here 描述的类似错误。
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多