Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在设置在Window.Resources中声明为静态资源的MenuItem的DataContext_C#_Wpf_Xaml_Datacontext_Menuitem - Fatal编程技术网

C# 正在设置在Window.Resources中声明为静态资源的MenuItem的DataContext

C# 正在设置在Window.Resources中声明为静态资源的MenuItem的DataContext,c#,wpf,xaml,datacontext,menuitem,C#,Wpf,Xaml,Datacontext,Menuitem,我有这个上下文菜单,我在我的树视图的不同数据模板上使用它 <Window.Resources> <ContextMenu x:Key="mnuContextTreeView"> <ContextMenu.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{StaticResour

我有这个上下文菜单,我在我的树视图的不同数据模板上使用它

<Window.Resources>
    <ContextMenu x:Key="mnuContextTreeView">
        <ContextMenu.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{StaticResource mnuRun}" />
                <Separator />
                <CollectionContainer Collection="{StaticResource mnuResults}" />
                <Separator />
                <MenuItem Name="mnuFlagContext" Command="local:MainWindow.MarkFlagged"
                    DataContext="" Visibility="{Binding Path=Flagged, Mode=OneWay,
                    Converter={StaticResource boolToCollapsedVisibilityConverter}}"  />
                <!-- I would like to set the DataContext of this one, so it could 
                     be hidden based on a property of the underlying ItemGroup or 
                     ItemType in the TreeView -->
                <CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
            </CompositeCollection>
        </ContextMenu.ItemsSource>
    </ContextMenu>
</Window.Resources>

使用上述关联菜单的TreeView:

<TreeView Name="myTreeView" DataContext="{Binding ElementName=mainWindow, 
    Path=RootElement}" ItemsSource="{Binding}">
    <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type logic:ItemGroup}" 
                ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" Foreground="Blue"
                    ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type logic:ItemType}">
                <TextBlock Text="{Binding Name}" Foreground="Red"
                    ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
            </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>


如何设置名为mnuFlagContext的MenuItem的DataContext,以便根据树视图中基础ItemGroup或ItemType的属性将其隐藏?

通过以下方法解决该问题(最终绑定的是标题而不是可见性,但与解决方案无关):

1) 将菜单分离为单独的静态资源:

    <collections:ArrayList x:Key="mnuToggleFlag" x:Shared="False">
        <MenuItem Command="local:MainWindow.ToggleFlag" 
            Header="{Binding Path=Flagged, Mode=OneWay, 
            Converter={StaticResource flaggedToHeaderConverter}}" />
    </collections:ArrayList>

因此,如果要求获取
标记的
属性,该属性可从
treevieItem
DataContext
ContextMenu中的
MenuItem.Header

您可以尝试:

<ContextMenu x:Key="mnuContextTreeView" 
             DataContext="{Binding RelativeSource={RelativeSource Self},
                                   Path=PlacementTarget.DataContext}">
    <ContextMenu.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{StaticResource mnuRun}" />
            <Separator />
            <CollectionContainer Collection="{StaticResource mnuResults}" />
            <Separator />
            <MenuItem Header="{Binding Path=Flagged,
                                       Mode=OneWay, 
                                       Converter={StaticResource flaggedToHeaderConverter}}"   
                      Command="local:MainWindow.MarkFlagged" />
            <CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
        </CompositeCollection>
    </ContextMenu.ItemsSource>
</ContextMenu>

很抱歉,这看起来很奇怪,我真的不会向任何人推荐这种方法。如果它对您有效,但对任何未来的读者来说都是很好的,我建议使用
ContextMenu
PlacementTarget
属性来提取
TreeView
DataContext
,然后将其传递到
MenuItem
当然,但我不确定是否正确遵循了您的所有要求。我的理解是,您有两个
层次数据模板
,其中两个都使用相同的
上下文菜单
,但是如果父项是特定的
数据类型
,您希望其中的
菜单项
隐藏/折叠。这是正确的吗?是的,或者更准确地说(在我的原始帖子之后更改)绑定MenuItem的标题,而不是可见性。并且,
标记的位置是
,它在
树视图项的
DataContext
中吗?ItemGroup和ItemType对象有一个公共基类(称为ItemBase),ItemBase有一个名为Flag的布尔属性。
((MenuItem)((ArrayList)Resources["mnuToggleFlag"])[0]).DataContext = _actualItem;
<ContextMenu x:Key="mnuContextTreeView" 
             DataContext="{Binding RelativeSource={RelativeSource Self},
                                   Path=PlacementTarget.DataContext}">
    <ContextMenu.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{StaticResource mnuRun}" />
            <Separator />
            <CollectionContainer Collection="{StaticResource mnuResults}" />
            <Separator />
            <MenuItem Header="{Binding Path=Flagged,
                                       Mode=OneWay, 
                                       Converter={StaticResource flaggedToHeaderConverter}}"   
                      Command="local:MainWindow.MarkFlagged" />
            <CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
        </CompositeCollection>
    </ContextMenu.ItemsSource>
</ContextMenu>
<TreeView Name="myTreeView" DataContext="{Binding ElementName=mainWindow, 
    Path=RootElement}" ItemsSource="{Binding}">
    <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type logic:ItemGroup}" 
                ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" Foreground="Blue"
                    ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type logic:ItemType}">
                <TextBlock Text="{Binding Name}" Foreground="Red"
                    ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
            </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>