上下文菜单触发不开火树型视图 [英] Context Menu trigger not firing on TreeViewItem

查看:122
本文介绍了上下文菜单触发不开火树型视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序,我添加了一个TreeView控件类的数据绑定到TreeViewItems。我添加了一个上下文菜单的treeviewitems。该文本菜单的处理程序不点火。这里是树视图的XAML代码

In my WPF application, I have added a TreeView control binding class data to TreeViewItems. I have added a context menu to the treeviewitems. The Handler of the contextMenu is not firing. Here is the XAML code for TreeView.

<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两班。

These are the two classes that are binded to the 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

This member is bind to the TreeView

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

当我右键点击菜单项中,RenameCommand事件不会开火。
当我改变的处理程序被触发,但TreeView的未与绑定的数据填充。

When I right click on the menu items, the RenameCommand event is not firing. When I change the to the handler is fired but TreeView is not populated with the binded data.

请帮忙。
谢谢

Please help. Thanks

推荐答案

基本上我在这里失去了重要的代码在哪里与列表类,这里就是命令绑定到?
此外,您正在使用的CommandBinding。这不是一个普通的事件处理程序。这些都是在WPF两件事情。我认为你缺少的属性在您的DataContext您的命令 - 类

Basically I am missing important code here about where is your class with the List, where is the command you bind to ? Furthermore you are using CommandBinding. This is not like a normal event handler. These are two things in WPF. I think you are missing the Property for your command in your DataContext - class.

这篇关于上下文菜单触发不开火树型视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆