wpf mvvm treeview contexmenu

时间:2015-08-03 06:00:00

标签: c# wpf mvvm treeview

我正在学习WPF / MVVM,并坚持跟随...... 以下代码正在运作

<ListBox x:Name="listbox" DockPanel.Dock="Top" ItemsSource="{Binding Items}" DisplayMemberPath="Name" >
  <ListBox.ContextMenu>
      <ContextMenu DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
         <MenuItem Header="Show Selected" 
         Command="{Binding Path=DataContext.ShowSelectedCommand}"
         CommandParameter="{Binding Path=SelectedItems}"/>
      </ContextMenu>
  </ListBox.ContextMenu>
</ListBox>

但是当我在树视图中替换了列表框时,例如

<TreeView x:Name="tview"  DockPanel.Dock="Top"  DisplayMemberPath="Name" Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding Items}" >
                <TreeView.ContextMenu>
                    <ContextMenu  DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" StaysOpen="true">
                        <MenuItem Header="Show Selected" Command="{Binding DataContext.ShowSelectedCommand}"
                                  CommandParameter="{Binding Path=SelectedItems}">
                             </MenuItem>
                         </ContextMenu>
                </TreeView.ContextMenu>
            </TreeView>

ShowSelectedCommand没有被调用。你能解释一下那里有什么问题吗?我怎样才能让它发挥作用。非常感谢。

0 个答案:

没有答案