上下文菜单触发器没有在TreeViewItem上触发

问题描述:

在我的WPF应用程序中,我向TreeViewItems添加了一个TreeView控件绑定类数据。我已经向treeviewitems添加了一个上下文菜单。 contextMenu的处理程序没有开火。这里是TreeView的XAML代码。上下文菜单触发器没有在TreeViewItem上触发

<TreeView ItemsSource="{Binding pads}" Width="190"> 
        <TreeView.ItemContainerStyle> 
         <Style TargetType="{x:Type TreeViewItem}"> 
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
          <Setter Property="FontWeight" Value="Normal" /> 
          <Setter Property="ContextMenu"> 
           <Setter.Value> 
            <ContextMenu> 
             <MenuItem Header="Rename" Command="{Binding RenameCommand}"/> 
            </ContextMenu> 
           </Setter.Value> 
          </Setter> 
          <Style.Triggers> 
           <Trigger Property="IsSelected" Value="True"> 
            <Setter Property="FontWeight" Value="Bold" /> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </TreeView.ItemContainerStyle> 

        <TreeView.ItemTemplate> 
         <HierarchicalDataTemplate ItemsSource="{Binding Members}"> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding Name}" /> 
           <TextBlock Text=" [" Foreground="Blue" /> 
           <TextBlock Text="{Binding Members.Count}" Foreground="Blue" /> 
           <TextBlock Text="]" Foreground="Blue" /> 
          </StackPanel> 
          <HierarchicalDataTemplate.ItemTemplate> 
           <DataTemplate DataType="{x:Type vm:PadInfo}"> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="["></TextBlock> 
             <TextBlock Text="{Binding SlotID}" /> 
             <TextBlock Text="] ["></TextBlock> 
             <TextBlock Text="{Binding WellID}" /> 
             <TextBlock Text="]"></TextBlock> 
            </StackPanel> 
           </DataTemplate> 
          </HierarchicalDataTemplate.ItemTemplate> 
         </HierarchicalDataTemplate> 
        </TreeView.ItemTemplate> 

       </TreeView> 

这些是绑定到TreeView的两个类。

/// <summary> 
/// Class to hold the Pads info for a tree 
/// </summary> 
public class Pad 
{ 
    /// <summary> 
    /// Default Constructor 
    /// </summary> 
    public Pad() 
    { 
     this.Members = new ObservableCollection<PadInfo>(); 
    } 

    /// <summary> 
    /// Name of the pad 
    /// </summary> 
    public string Name { get; set; } 

    /// <summary> 
    /// Members of the pad 
    /// </summary> 
    public ObservableCollection<PadInfo> Members { get; set; } 
} 

/// <summary> 
/// Class to hold the well and slot IDs snapped to a pad 
/// </summary> 
public class PadInfo 
{ 
    /// <summary> 
    /// Slot ID 
    /// </summary> 
    public string SlotID { get; set; } 

    /// <summary> 
    /// Well ID 
    /// </summary> 
    public string WellID { get; set; } 
} 

}

该成员绑定到TreeView

public List<Pad> pads { get; set; } 

当我右键点击菜单项,在RenameCommand事件不会开火。 当我更改为处理程序被解雇但TreeView没有填充绑定数据。

请帮忙。 谢谢

基本上我在这里丢失重要代码关于你的类与列表在哪里,你绑定的命令在哪里? 此外,您正在使用CommandBinding。这不像普通的事件处理程序。这是WPF中的两件事情。我认为你在你的DataContext类中缺少你的命令的属性。

+0

当我使用TreeViewItem代替TreeView.ItemTemplate时,同样的命令被触发。所以我不认为问题是用Command或DataContext。 当我为此 并按F2相同的命令火灾。 – WAQ

+0

它不依赖于treeviewitem/template。命令和事件之间的区别在于,在WPF(或其他)中的事件中,您正在定义执行的方法。使用Command绑定你有一个属性。而这个属性包含了应该执行的动作。但是这里没有这个代码。 – Tintenfiisch

+0

这里是我的命令处理程序 private ICommand _RenameCommand; 公众的ICommand RenameCommand { 得到 { 如果(_RenameCommand == NULL){ _RenameCommand =新RelayCommand1((0)=> {MessageBox.Show( “重命名我”);}); } return _RenameCommand; } } – WAQ