的文本菜單的DataContext我有一個文本菜單綁定到的CommandBinding:獲取父
<MetroTreeView:MetroTreeView x:Name="tvTagList" Grid.Row="1" Margin="15, 0, 15, 0" SelectedItemChanged="tvTagList_SelectedTagChanged" ItemsSource="{Binding}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{x:Null}" BorderBrush="{DynamicResource ExtryzeAccentBrushSecondary}" BorderThickness="2" Foreground="White"
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Standard">
<MetroTreeView:MetroTreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type DataBind:TagClass}" ItemsSource="{Binding Children}" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<TextBlock>
<Run Text="{Binding TagClassMagic}" />
<Run Foreground="{StaticResource TextBrushSecondary}" Text=" - " />
<Run Foreground="{StaticResource TextBrushSecondary}" Text="{Binding Path=Description, Converter={StaticResource IValueTagDescriptionCleanup}}" />
</TextBlock>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Dump Tag List..." Click="DumpClassTagList" />
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type DataBind:TagEntry}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<StackPanel.ContextMenu>
<ContextMenu>
<ContextMenu.CommandBindings>
<CommandBinding Command="{x:Static DataBind:HaloMap.CommandTagBookmarking }"
CanExecute="CommandTagBookmarking_CanExecute"
Executed="CommandTagBookmarking_Executed"
/>
</ContextMenu.CommandBindings>
<MenuItem Header="Bookmark Tag" Foreground="White" Command="{x:Static DataBind:HaloMap.CommandTagBookmarking}" CommandParameter="{Binding}" />
</ContextMenu>
</StackPanel.ContextMenu>
<TextBlock Text="{Binding TagFileName}" />
</StackPanel>
</DataTemplate>
</MetroTreeView:MetroTreeView.Resources>
<MetroTreeView:MetroTreeView.ItemContainerStyle>
<Style TargetType="{x:Type MetroTreeView:MetroTreeViewItem}" BasedOn="{StaticResource {x:Type MetroTreeView:MetroTreeViewItem}}">
<EventSetter Event="MouseDoubleClick" Handler="tvTagList_ItemDoubleClick" />
</Style>
</MetroTreeView:MetroTreeView.ItemContainerStyle>
但是當我運行CanExecute
命令,每一個嘗試訪問的DataContext總是遇到空:
private void CommandTagBookmarking_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
var a = e.Source;
var b = ((MenuItem)a).DataContext;
var c = ((ContextMenu) ((MenuItem) e.Source).Parent).DataContext;
}
不, 「變種C =((文本菜單)((菜單項)e.Source).Parent).DataContext;」仍然返回null。 – 2013-03-15 20:49:19
@AlexReed我建議你使用'Snoop'來檢查正在運行的應用程序,看看差距在哪裏。 – 2013-03-15 20:55:12
Snoop甚至不承認我的應用程序正在運行。 – 2013-03-15 21:25:05