WPF - 访问命令的 TreeViewItem 的根 UserControl DataContext

问题描述 投票:0回答:1

我正在尝试执行绑定到子 TreeViewItem 的根父 UserControl 的 ViewModel 中存在的命令。这是来自绑定到根 TreeViewItems 的 ContextMenu。树视图代码:

<Grid x:Name="GistBrowserGRD" Grid.Row="0" Margin="2">
    <Grid.Resources>
        <HierarchicalDataTemplate DataType="{x:Type viewmodels:GistViewModel}" ItemsSource="{Binding Path=GistFiles}">
            <StackPanel Orientation="Horizontal">

                <TextBlock VerticalAlignment="Center" Margin="2,0,2,0"
                    Text="{Binding GistFiles, Converter={StaticResource GistFilesToFirstFilenameConverter}}"></TextBlock>

                <StackPanel.ContextMenu>
                    <ContextMenu Style="{StaticResource ContextMenu}">
                        <MenuItem Header="Delete Gist" Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
                                  Style="{StaticResource MenuItem}" 
                                  Command="{Binding 
                                        RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type viewmodels:MainWindowViewModel}},
                                        Path=DataContext.DeleteGistCMD}">
                            <MenuItem.Icon>
                                <imaging:CrispImage Moniker="{x:Static catalog:KnownMonikers.DeleteListItem}"/>
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </StackPanel.ContextMenu>

            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type viewmodels:GistFileViewModel}">
            <StackPanel Orientation="Horizontal">
                <imaging:CrispImage VerticalAlignment="Center" Width="16" Height="16" Margin="0,0,4,0"
                                    Moniker="{x:Static catalog:KnownMonikers.Document}" />
                <TextBlock  VerticalAlignment="Center" Margin="2,0,2,0" Text="{Binding Filename}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <TreeView x:Name="GistsTV" ItemsSource="{Binding Gists}" BorderThickness="0" Background="Transparent">
        <TreeView.Resources>
            <!-- Style the inactive selection the same as active -->
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                Color="#808080" Opacity="0.4"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
                Color="{DynamicResource {x:Static SystemColors.HighlightTextColorKey}}"/>
        </TreeView.Resources>
        <i:Interaction.Behaviors>
            <behaviours:BindableSelectedItemBehavior SelectedItem="{Binding SelectedGistVmItem, Mode=TwoWay}" />
        </i:Interaction.Behaviors>
    </TreeView>
</Grid>

MainWindowViewModel
是 MainWindow 的 DataContext,其中存在我要定位的命令。我已经尝试了用于绑定的relativesource的各种排列,尽管xaml智能感知通过提供命令作为选项来表明绑定是正确的,但它不会在选择相关的ContextMenuItem时触发。

有什么想法吗?

wpf command datacontext hierarchicaldatatemplate relativesource
1个回答
0
投票

我能找到的最优雅的解决方案避免了代码隐藏添加或 ViewModel 架构重新设计是这样的:

https://stackoverflow.com/a/33485960/3472225

它提供了一个扩展,可以检索根 DataContext 并生成整洁的 xaml。唯一的缺点是智能感知不显示可用的命令,但仍然可以确认是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.