在TreeView中绑定ContextMenu [英] Binding a ContextMenu in a TreeView

查看:135
本文介绍了在TreeView中绑定ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要一个有关HierarchicalDataTemplate中的CONTEXTMENU的技巧....
我有一个像这样的HierarchicalDataTemplate:

Hi,
I need a trick about CONTEXTMENU in a HierarchicalDataTemplate....
I''ve a HierarchicalDataTemplate like this:

<TreeView ItemsSource="{Binding viewModels}">
    <TreeView.ItemContainerStyle>
        <!-- This Style binds a TreeViewItem to a TreeViewItemViewModel. -->
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:custRootViewModel}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=name}">
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <!--- a lot of MenuItem -->
                    </ContextMenu>
                </TextBlock.ContextMenu>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>



我需要一个特定的ContextMenu与Treeview中的节点关联...
但我无法绑定ContextMenu.
在custRootViewModel类中,我有两个属性:
孩子们:谁告诉我有关孩子们的事情(这是工作!)
动作:谁告诉我有关menuitem的信息(我不知道如何绑定它们...)


如何构建ContextMenu?
谢谢...



I need a specific ContextMenu associate with a node in Treeview...
but I can''t Binding ContextMenu.
In a custRootViewModel Class I''ve two property:
Children: who tell me about children (THIS WORK!)
Actions: who tell me about menuitem (I don''t know how to bindin them...)


How can I build ContextMenu?
Thanks...

推荐答案

我不太了解您的问题,但是让我看看我是否正确.您需要根据ViewModel上的操作向项目的ContextMenu添加内容,对吧?
由于ContextMenu是从ItemsItem扩展的MenuBase扩展而来的,因此您可以使用ItemsSourceProperty(这是DependencyProperty,因此接受Bindings)动态地向其添加itens,因此ContextMenu您可以添加ItemsSource ="{Binding ContextMenuItems}"和ContextMenuItems属性应该是一个新Type的集合,其中包含应该在ContextMenu的MenuItem中写入的文本以及将由它执行的ICommand,如:

I didn''t quite understand your question, but let''s see if I got it right. You need to add content to the ContextMenu of the item based on the actions on the ViewModel, right?
Since the ContextMenu extends from MenuBase that extends from ItemsControl, you can add itens dynamically to it by using the ItemsSourceProperty (which is a DependencyProperty, so accept Bindings) so the ContextMenu you add a ItemsSource="{Binding ContextMenuItems}" and the ContextMenuItems property should be a collection of a new Type that contains the text that should be writen in the MenuItem of the ContextMenu and the ICommand that will be executed by it like:

public class Item : INotifyPropertyChanged
{
    public ICommand Command
    {
        get;
        private set;
    }

    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            if (_Name != value)
            {
                _Name = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}



然后,需要将Item的DataTemplate添加到ContextMenu中,该模板类似于:



Then you need to add to the ContextMenu a DataTemplate for the Item that will be something like:

<ContextMenu.ItemTemplate>
    <DataTemplate>
        <MenuItem Header="{Binding Name}" Command="{Binding Command}"/>
    </DataTemplate>
</ContextMenu.ItemTemplate>



那么您的Xaml代码将类似于:



then your Xaml Code will be something like:

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:custRootViewModel}" ItemsSource="{Binding Path=Children}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=name}">
        <TextBlock.ContextMenu>
            <ContextMenu ItemsSource="{Binding Path=ContextMenuItems}">
                <ContextMenu.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding Name}" Command="{Binding Command}"/>
                    </DataTemplate>
                </ContextMenu.ItemTemplate>
            </ContextMenu>
        </TextBlock.ContextMenu>
            </TextBlock>
        </StackPanel>
    </HierarchicalDataTemplate>
</TreeView.Resources>



然后,对于Children集合中的每个项目,您都有一个ContextMenu的项目集合,并使用Command属性可以触发每种特定方案所需的操作.

仅出于记录目的,您需要将Command添加到控件的CommandBindings(这是CommandBinding的集合)中,此操作可以通过使用静态方法CommandManager.RegisterClassCommandBinding(控件的类型,新的CommandBinding(ICommand)来完成.命令,ExecutedRoutedEventHandler方法,CanExecuteRoutedEventHandler方法)或通过调用CommandBinding集合的Add方法(UIElement.CommandBindings).

有关更多知识,请阅读:
http://msdn.microsoft.com/en-us/library/system. windows.input.commandbinding.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/system. windows.uielement.commandbindings.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.registerclasscommandbinding.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/system. windows.controls.contextmenu.aspx [ ^ ]

希望这会有所帮助.
最好的

劳尔·迈纳迪(Raul Mainardi Neto)



Then for each Item in the Children Collection you have a collection of Items for the ContextMenu and using the Command Property you can trigger the action that you need for every specific scenario.

Just for the record you need to add the Command to the CommandBindings (which is a Collection of CommandBinding) of the control for this to work this is done by the use of either the static method CommandManager.RegisterClassCommandBinding(Type typeofthecontrol, new CommandBinding(ICommand command, ExecutedRoutedEventHandler Method, CanExecuteRoutedEventHandler Method)) or by the call of the Add method of the CommandBinding collection (UIElement.CommandBindings).

For further knowledge read:
http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.uielement.commandbindings.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.registerclasscommandbinding.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.aspx[^]

Hope this helps.
All best

Raul Mainardi Neto


这篇关于在TreeView中绑定ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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