在XAML绑定一个TreeView与文本菜单 [英] Binding a TreeView with ContextMenu in Xaml

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

问题描述

我是pretty新XAML和需要一些建议。

I'm pretty new to Xaml and need some advise.

一个TreeView控件应绑定到一个分层的对象结构。树视图应该有一个上下文菜单,这是具体的每个对象类型

A TreeView should be bound to a hierarchical object structure. The TreeView should have a context menu, which is specific for each object type.

我试过以下内容:

<TreeView>
  <TreeView.Resources>
    <DataTemplate x:Key="RoomTemplate">
      <TreeViewItem Header="{Binding Name}">
        <TreeViewItem.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Open" />
            <MenuItem Header="Remove" />
          </ContextMenu>
        </TreeViewItem.ContextMenu>
      </TreeViewItem>
    </DataTemplate>
  </TreeView.Resources>

  <TreeViewItem Header="{Binding Name}" Name="tviRoot" IsExpanded="True" >

  <TreeViewItem Header="Rooms"  
                ItemsSource="{Binding Rooms}"
                ItemTemplate="{StaticResource RoomTemplate}">
    <TreeViewItem.ContextMenu>
      <ContextMenu>
        <MenuItem Header="Add room"></MenuItem>
      </ContextMenu>
    </TreeViewItem.ContextMenu>
  </TreeViewItem>
</TreeViewItem>

但有了这个标记的行为如预期,但孩子的物品(房间)是缩进的太多了。

But with this markup the behavior is as intended, but the child items (the rooms) are indented too much.

反正所有比南样品我可以找到使用TextBlock的,而不是树型视图中的DataTemplate,但不知道如何将文本菜单有集成。

Anyway all the bining samples I could find use TextBlock instead of TreeViewItem in the DataTemplate, but wonder how to integrate the ContextMenu there.

推荐答案

您通常不会创建一个包含树型视图一个DataTemplate,因为基础设施的结合将会为您创建的TreeViewItem - 你的DataTemplate需要做的一切都是指定哪些应显示为树型视图的的内容的。这就是为什么样你发现,而不是在DataTemplate中TreeViewItems使用的TextBlocks。

You would not normally create a DataTemplate containing a TreeViewItem, because the binding infrastructure will be creating the TreeViewItem for you -- all your DataTemplate needs to do is specify what should be displayed as the content of the TreeViewItem. That's why the samples you've found use TextBlocks instead of TreeViewItems in the DataTemplate.

我怀疑使用树型视图,而不是TextBlock中引起过多的缩进,因为你有你的DataTemplate内的另一个(自动)树型视图A(手动创建)树型视图(这将产生缩进一个级别)(这将产生缩进的另一个层面) 。因此,使用一个TextBlock代替的TreeViewItem应该治愈这种。整合的ContextMenu不应该是一个问题,因为TextBlock的有一个ContextMenu属性了。

I suspect the use of TreeViewItem rather than TextBlock causes the excessive indenting because you have a (manually created) TreeViewItem in your DataTemplate (which incurs one level of indent) inside another (automatic) TreeViewItem (which incurs another level of indent). Therefore, using a TextBlock instead of a TreeViewItem should cure this. Integrating the ContextMenu shouldn't be an issue because TextBlock has a ContextMenu property too.

所以,你应该能够只是改变你的DataTemplate如下:

So you should be able to just change your DataTemplate as follows:

<DataTemplate x:Key="RoomTemplate">
  <TextBlock Text="{Binding Name}">
    <TextBlock.ContextMenu>
      <ContextMenu>
        <MenuItem Header="Open" />
        <MenuItem Header="Remove" />
      </ContextMenu>
    </TextBlock.ContextMenu>
  </TextBlock>
</DataTemplate>

顺便提及为的TreeView它是通常使用HierarchicalDataTemplate而非普通的DataTemplate因为这允许用于经由HierarchicalDataTemplate.ItemsSource属性的项目的多个级别。这可能并不需要在思想上您的方案。

Incidentally for TreeViews it is common to use a HierarchicalDataTemplate rather than a plain DataTemplate because this allows for multiple levels of items via the HierarchicalDataTemplate.ItemsSource property. This may not be required in your scenario though.

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

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