上下文菜单触发器未在TreeViewItem上触发

时间:2013-07-07 09:04:47

标签: c# wpf xaml treeview wpf-controls

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

<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不会使用绑定数据填充。

请帮忙。 感谢

1 个答案:

答案 0 :(得分:0)

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